0

In JavaScript I can assign onclick by traditional way:

button.onclick = engineStop();

But how can I assign to onclick a function with parameter(s)?

This does not work >>

button.onclick = engineStop(this);

The function needs to receive this parameter to know which button has been clicked on.

Please advice... (no jQuery)

2 Answers 2

3
button.onclick = function() {engineStop(this);};

Using an anonymous function.

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

Comments

2

Like this:

function engineStop(param){
    return function(){
        //engineStop function's body here,
        //which uses 'param' argument
    };
}
button.onclick = engineStop(this);

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.