2

I have this code:

var bt = document.getElementById("bt");
bt.onclick = randomFunc;

Now, how can I pass some parameters to randomFunc?

1 Answer 1

4

Create a new function which calls randomFunc and specify the parameters there.

bt.onclick = function () {
    randomFunc(foo, bar, baz);
};

Or, if you can limit support to browsers which support bind, use that method to create your function:

bt.onclick = randomFunc.bind(this, foo, bar, baz);
Sign up to request clarification or add additional context in comments.

2 Comments

While I'd recommend addEventListener() over onclick =, it doesn't have any features to help with the problem of binding arguments.
@Quentin indeed - I just couldn't leave the OP's use of obsolete DOM0 event handling go unchallenged... ;-)

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.