Connecting Tech Pros Worldwide Help | Site Map

Pass variable to addEventListener

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: 4 Weeks Ago
Expand|Select|Wrap|Line Numbers
  1. function addValue (i) { 
  2.    var field = document.getElementById('bar').innerHTML;
  3.    document.getElementById('bar').innerHTML =  field + 'Value=' + i;
  4. }
  5.  
  6. document.getElementById('foo').addEventListener('click', addValue(5), false);

This is supposed to add Value=5 once the element is clicked but instead it adds Value=5 when the page loads, and doesn't react to clicks.

Works perfectly when no variable is passed.

Expand|Select|Wrap|Line Numbers
  1. function addValue () { 
  2.    var field = document.getElementById('bar').innerHTML;
  3.    document.getElementById('bar').innerHTML =  field + 'Value=5';
  4. }
  5.  
  6. document.getElementById('foo').addEventListener('click', addValue, false);
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: 4 Weeks Ago

re: Pass variable to addEventListener


Quote:

Originally Posted by pdknsk View Post

Works perfectly when no variable is passed.

yupp, that’s how addEventListener() works (see ref.), you pass a function reference (aka the function name). you have to use a closure to pass functions with parameters.
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: 4 Weeks Ago

re: Pass variable to addEventListener


Thanks. How would I do this?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: 4 Weeks Ago

re: Pass variable to addEventListener


(about Closures)
Expand|Select|Wrap|Line Numbers
  1. var aClosure = function() {
  2.     addValue(5);
  3. }
  4. // or
  5. function aClosure() {
  6.     addValue(5);
  7. }
  8.  
  9. element.addEventListener("click", aClosure);
Newbie
 
Join Date: Oct 2009
Posts: 5
#5: 4 Weeks Ago

re: Pass variable to addEventListener


Thanks, this works for passing variables, but there is still a minor problem. It seems the passed variable is not registered or stored with the Listener when it is created, but only when the click event is executed.

This shows Value=5 for all foo_* but is supposed to show Value=1 for foo_1 and so forth.

Expand|Select|Wrap|Line Numbers
  1. function addValue(i) { 
  2.     var field = document.getElementById('bar').innerHTML;
  3.     document.getElementById('bar').innerHTML =  field + 'Value=' + i';
  4. }
  5.  
  6. for (k=0;k<5;k++) {
  7.     function addValueExt() {
  8.         addValue(k);
  9.     }
  10.     document.getElementById('foo_'+k).addEventListener('click', addValueExt, false);
  11. }
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: 4 Weeks Ago

re: Pass variable to addEventListener


Quote:

Originally Posted by pdknsk View Post

This shows Value=5 for all foo_* but is supposed to show Value=1 for foo_1 and so forth.

nope, that’s not how Closures work. Closures are named as such because they preserve variable values from outside scope. in your case the function addValueExt is called (via reference) on the event and at that time, the variable k always has the value 5 (because the loop already finished).
Newbie
 
Join Date: Oct 2009
Posts: 5
#7: 4 Weeks Ago

re: Pass variable to addEventListener


I'll have to figure out a way to pass the corresponding value then.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#8: 4 Weeks Ago

re: Pass variable to addEventListener


save the value in the HTMLElement and access this value from inside the addValue function.
Newbie
 
Join Date: Oct 2009
Posts: 5
#9: 4 Weeks Ago

re: Pass variable to addEventListener


I solved this using a similar method, without passing a variable to addEventListener.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#10: 4 Weeks Ago

re: Pass variable to addEventListener


Quote:

Originally Posted by pdknsk View Post

without passing a variable to addEventListener.

as you already noticed, you can’t pass variables to addEventListener
Reply