[thelist] unobtrusive JS: adding events with parameters?

Iain iain at firelightning.com
Thu Jan 12 10:44:36 CST 2006


Try this:

function kButtonBool(e) {
	var obj = (!window.event) ? e.target : window.event.srcElement;
	// Rest of your code...
}

Iain

Tom Dell'Aringa wrote:
> Hello listers, 
> 
> I'm using Scott Andrew's faithful addEvent() method[1] to add an event to some images at load time. My init function has this clause: 
> 
> function kButtonInit() 
> { 
>   var inputs = document.getElementsByTagName('img'); 
>   for(var y=0; y { 
>     if(inputs[y].className == "kbutbool") 
>     { 
>       addEvent(inputs[y], "click", kButtonBool); 
>     } 
>   } 
> } 
>  
> The event gets added just fine. The problem is, I need to send one argument to kButtonBool, namely a 'this' reference to the img object itself like so:
>  
> onclick="kButtonBool(this)"
>  
> Does anyone have any techniques for adding functions to handlers without placing them directly in the code?
>  
> Thanks
>  
> Tom
> 
> [1] http://www.scottandrew.com/weblog/articles/cbs-events




More information about the thelist mailing list