Solved

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

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

Technology Partners: 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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

752 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