[thelist] unobtrusive JS: adding events with parameters?

Howard Jess howard at dhitechnologies.com
Thu Jan 12 15:35:22 CST 2006


Tom Dell'Aringa <pixelmech at yahoo.com> wrote:

> 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)"

Untested, but illustrative?

    function kButtonInit() { 
        var inputs = document.getElementsByTagName('img');
        function addClicker(elm) {
            addEvent(elm,"click",function () {kButtonBool.call(elm)});
        }
        for (var y=0; y<inputs.length; y++) 
            if (inputs[y].className == "kbutbool")
                addClicker(inputs[y]);
    } 
    
(The "call" method of Function objects takes as its first argument
the object to be treated as "this" within the method.)
 

hj



More information about the thelist mailing list