Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick assigned function with parameters

I'm not sure if this has been asked before because I don't know what it's called.

But why wouldn't a method like this work? Below is just a general example

<script>
document.getElementById('main_div').onclick=clickie(argument1,argument2);

function clickie(parameter1,parameter2){
 //code here
}

</script>

The code above would work fine if the event handler was assigned without parameters, but with parameters, it doesn't work. I think I read online that to overcome this problem, you could use closures. I'm assuming it's because of the parentheses ( ) that is calling the function immediately instead of assigning it to the event?

like image 473
user3029001 Avatar asked Jun 05 '14 02:06

user3029001


People also ask

How to pass parameters in onClick event?

To pass an event and parameter onClick in React: Copied! We set the onClick prop on the button element to an inline arrow function. The arrow function takes the event object and calls the handleClick function passing it the event and a parameter.

How do you pass multiple parameters in onClick function in react?

But if in any function you want to pass any extra parameter, then you need to use either arrow function or bind that function by using . bind(this, parameter) . Note: Another thing that you need to change here is, the way you are updating the state values, You should never mutate state variable directly.

How to pass input value in onClick function JavaScript?

It's very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or single ” quote in the function.

How to pass object in onClick function in HTML?

Just stringify the object using JSON. strigify(obj) and pass into the onclick function as parameter and it will be parsed directly and received in the function as Object.


2 Answers

Because you're calling the function immediately and returning the result, not referencing it.

When adding the parenthesis you call the function and pass the result back to onclick

document.getElementById('main_div').onclick = clickie(); // returns undefined

so it's actually equal to writing

document.getElementById('main_div').onclick = undefined;

which is not what you want, you want

document.getElementById('main_div').onclick = clickie;

but then you can't pass arguments, so to do that you could use an anonymous function as well

document.getElementById('main_div').onclick = function() {
    clickie(argument1,argument2);
}

or use bind

document.getElementById('main_div').onclick = yourFunc.bind(this, [argument1, argument2]);

It is however generally better to use addEventListener to attach event listeners, but the same principle applies, it's either (without arguments)

document.getElementById('main_div').addEventListener('click', clickie, false);

or bind or the anonymous function to pass arguments etc.

document.getElementById('main_div').addEventListener('click', function() {
    clickie(argument1,argument2);
}, false);
like image 181
adeneo Avatar answered Sep 21 '22 04:09

adeneo


The easiest way is:

yourElement.onclick = yourFunc.bind(this, [arg1, arg2]);

function yourFunc (args, event) {
    // here you can work with you array of the arguments 'args'
}
like image 38
Egor Avatar answered Sep 23 '22 04:09

Egor