Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do i pass an argument to an eventListener in a quick easy way?

Posted on 2011-09-27
3
254 Views
Last Modified: 2012-05-12
Hi

I'm trying to solve this thing with passing variables with eventListeners..
Why is it so hard? Seriously?

currentObject is null when i'm trying to alert this and I've come to understand that it has something with closure to do. But isn't there som way to get the variables in there anyway?

I just want a way to pass arguments to a function that i call when i press an html-object.
It would probably work with setting ahref to the object,, or perhaps giving the html-object a attribute (nr), and then use .this to get it's attribute. But is this really neccessary?


function outerFunction() {
	var currentObject = g.objectHandler.first;
	var newObjectTitle;
	var newTextNode;
	
	while(currentObject != null) {

		newObjectTitle = document.createElement('p');
		newTextNode = document.createTextNode(currentObject.title);
		newObjectTitle.appendChild(newTextNode);
		newScrollBox.appendChild(newObjectTitle);

		newObjectTitle.addEventListener('click', function(){innerFunction(currentObject.nr)},false);
		
		////////////////////////////////////////
		
		currentObject = currentObject.next;		
	}
}


function innerFunction(nr) {
	alert(nr);
}

Open in new window

0
Comment
Question by:walkman69
3 Comments
 
LVL 10

Expert Comment

by:JPJ78
ID: 36715037
Maybe you can use.

newObjectTitle.addEventListener('click', function(){ eval("innerFunction(" + currentObject.nr + ");");}, false);
0
 
LVL 6

Accepted Solution

by:
mattjp88 earned 500 total points
ID: 36715761
use anonymous functions:

newObjectTitle.addEventListener('click', function(co){  return function(){  innerFunction(co.nr)  } }(currentObject),false);

Open in new window


It's all about scope.  The function(co) function will take in the object you are referencing and then return a new function that will get called when the event triggers.

-Matt
0
 
LVL 2

Author Closing Comment

by:walkman69
ID: 37123492
Nice.. thanx! =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question