Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox style onclick arguments in IE

In firefox when you add an onclick event handler to a method an event object is automatically passed to that method. This allows, among other things, the ability to detect which specific element was clicked. For example

document.body.onclick = handleClick;

function handleClick(e)
{
    // this works if FireFox
    alert(e.target.className);
}

Is there any way to approximate this in IE? i need to be able to detect which element is clicked from an event handler on the body element.

like image 998
Jack Ryan Avatar asked Dec 18 '22 10:12

Jack Ryan


1 Answers

Here is how would I do it in case I cannot use jQuery

document.body.onclick = handleClick;

function handleClick(e)
{
    //If "e" is undefined use the global "event" variable
    e = e || event;

    var target = e.srcElement || e.target;
    alert(target.className);
}

And here is a jQuery solution

$(document.body).click(function(e) {
  alert($(this).attr("class"));
});
like image 106
Atanas Korchev Avatar answered Jan 02 '23 07:01

Atanas Korchev