Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters in Javascript onClick event

I'm trying to pass a parameter in the onclick event. Below is a sample code:

<div id="div"></div>  <script language="javascript" type="text/javascript">    var div = document.getElementById('div');     for (var i = 0; i < 10; i++) {        var link = document.createElement('a');        link.setAttribute('href', '#');        link.innerHTML = i + '';        link.onclick=  function() { onClickLink(i+'');};        div.appendChild(link);        div.appendChild(document.createElement('BR'));        }     function onClickLink(text) {        alert('Link ' + text + ' clicked');        return false;        }     </script> 

However whenever I click on any of the links the alert always shows 'Link 10 clicked'!

Can anyone tell me what I'm doing wrong?

Thanks

like image 788
fatcatz Avatar asked Aug 16 '10 17:08

fatcatz


People also ask

How do you pass a function parameter on onClick?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How do you pass two values on onClick?

If you want to call a function when the onclick event happens, you'll just want the function name plus the parameters. Then if your parameters are a variable (which they look like they are), then you won't want quotes around them.

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 do you pass parameters to a function in react JS?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...


2 Answers

This happens because the i propagates up the scope once the function is invoked. You can avoid this issue using a closure.

for (var i = 0; i < 10; i++) {    var link = document.createElement('a');    link.setAttribute('href', '#');    link.innerHTML = i + '';    link.onclick = (function() {       var currentI = i;       return function() {            onClickLink(currentI + '');       }    })();    div.appendChild(link);    div.appendChild(document.createElement('BR')); } 

Or if you want more concise syntax, I suggest you use Nick Craver's solution.

like image 95
Jamie Wong Avatar answered Oct 07 '22 00:10

Jamie Wong


This is happening because they're all referencing the same i variable, which is changing every loop, and left as 10 at the end of the loop. You can resolve it using a closure like this:

link.onclick = function(j) { return function() { onClickLink(j+''); }; }(i); 

You can give it a try here

Or, make this be the link you clicked in that handler, like this:

link.onclick = function(j) { return function() { onClickLink.call(this, j); }; }(i); 

You can try that version here

like image 31
Nick Craver Avatar answered Oct 07 '22 01:10

Nick Craver