Search code examples
javascriptmootoolselementgetelementsbytagname

accessing element attributes with mootools selector


I'm trying to attach a mouseover event to all img elements of a gallery in a page (using Mootools). This is easy enough using something like

$$('img.mygalleryclass').addEvents({
mouseover: function(){
    alert(id);
    }
});

My question is, how do I alert the id of the element referenced in the $$ 'loop'? That "alert(id)" returns an error every time since id is not defined.

Thanks!


Solution

  • Pass the event argument to the mouseover function and then get the id of the target:

    $$('img.mygalleryclass').addEvents({
        mouseover: function(e) {
            alert(e.target.id);
        }
    });
    

    Here's an example.