Tuesday, September 30, 2008

Javascript catching mouse click event and know what DOM element is being clicked

Asking some great encoders in irc.freenode at #javascript channel, people are having an extensive help to me.
I got this code working, but here I use the mootools JS Library but still the same as native javascript syntax.

$(document).addEvent('click', function(e){
console.info(e.target.get('id'));
});


// This works in IE
$(document).attachEvent('onclick', function(e){
alert(e.srcElement.id) // gives the id of the element being clicked
});

Through it, it does prints or gives me the id of the element being clicked. It's really cool knowing this technique.


No comments: