473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Pass variable to addEventListener

5
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);
Oct 18 '09 #1
9 6451
Dormilich
8,658 Expert Mod 8TB
@pdknsk
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.
Oct 18 '09 #2
pdknsk
5
Thanks. How would I do this?
Oct 18 '09 #3
Dormilich
8,658 Expert Mod 8TB
(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);
Oct 18 '09 #4
pdknsk
5
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. }
Oct 18 '09 #5
Dormilich
8,658 Expert Mod 8TB
@pdknsk
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).
Oct 18 '09 #6
pdknsk
5
I'll have to figure out a way to pass the corresponding value then.
Oct 18 '09 #7
Dormilich
8,658 Expert Mod 8TB
save the value in the HTMLElement and access this value from inside the addValue function.
Oct 18 '09 #8
pdknsk
5
I solved this using a similar method, without passing a variable to addEventListener.
Oct 19 '09 #9
Dormilich
8,658 Expert Mod 8TB
@pdknsk
as you already noticed, you can’t pass variables to addEventListener
Oct 19 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: bmgz | last post by:
sorry for the stupid question, bu6 I haven't been able to find the answer anywhere.. consider this useless function which assigns an object to a var.. function(myParam){ var select1 =...
11
by: me | last post by:
I have got all my pages to comply with the W3C validator, except this one line as below. I need to keep the line (or the functionalilty) but it would be nice to implement it in a way that gives...
4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
6
by: soulmach | last post by:
Using Firefox, I am trying to access a link within my HTML document, then use addEventListener() to add a click event to that link. I am using two external JS files. ajaxModal.js has the...
6
by: Max | last post by:
i have a event bind function like this(though it is not so robust): bind$=function(o,evt,fn,cb){ var aE='attachEvent'; var aEL='addEventListener'; if(!o&&o){ return o(evt,fn,!!cb); } return...
2
by: JWest46088 | last post by:
I'm making a contact form, and I keep getting two errors when I try previewing it in a web browser. ReferenceError: Error #1065: Variable Button is not defined. ReferenceError: Error #1065:...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.