Variable Passing

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
96a2107
Forum Newbie
Posts: 1
Joined: Wed Oct 17, 2007 1:48 pm

Variable Passing

Post by 96a2107 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This function should find the current clicked element, but Im finding it hard to return the variable 'x' which seems its outside the scope of the nested 'onmousedown' function, has anyone got a solution for this?

[syntax="javascript"]
function getCurrentID()
{
var x;

	el=document.body.getElementsByTagName('*');
	for(c=0;c<el.length;c++) 
	{
		el[c].onmousedown=function() 
		{
		x=this.id;
		}
	}
return x
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe you are looking for a "closure"-type solution.

http://en.wikipedia.org/wiki/Closure_%2 ... science%29
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Take a look at Douglas Crockford's Survey of the JavaScript Programming Language, particularly the Vars and Closure sections, althought the entire article is awesome.
Post Reply