3

I am trying to convert a Java program into HTML and JS, and in the Java code I call a method using an int from that class. Is this possible with HTML and JS? Can I write onload="myFunc(i + 1)" or something to that effect?

If you need it, the Java code looks like this:

public int i = 0;

public String chooseACard(int j) {
    if(j > cards.length - 1) j = 0;
    return cards[j].front;
}

and it is called by:

ActionListener changeFrontListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == nextCard) {
                textBox.setText(vc.chooseACard(vc.i + 1));
                if(vc.i + 1 > vc.cards.length - 1) vc.i = 0;
                else vc.i++;
            }
        }

    };

Can I do anything like this in HTML and JavaScript?

2
  • When should function be called? Commented Apr 12, 2017 at 17:04
  • It doesn't matter, someone else has solved it, but thanks anyway Commented Apr 12, 2017 at 17:06

1 Answer 1

1

Yes, you can do it like shown below. Basically, You can assign the handler of the event a function name or a function expression.

var i = 9;

function myFunc(t){
console.log(t);}
<button id="button1" onclick="myFunc(i + 1);">TestMe</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.