Solved

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

Posted on 2011-09-27
3
251 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now