Page 1 of 1

Variable Passing

Posted: Wed Oct 17, 2007 1:56 pm
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]

Posted: Mon Oct 22, 2007 1:36 am
by feyd
I believe you are looking for a "closure"-type solution.

http://en.wikipedia.org/wiki/Closure_%2 ... science%29

Posted: Mon Oct 22, 2007 2:51 am
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.