Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-27
3
Medium Priority
?
261 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

926 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