Solved

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

Posted on 2011-09-27
3
257 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
[X]
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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

630 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