Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE e.target.id is not working

document.click = check;

function check(e)
{ 
    var obj = document.getElementById('calendar_widget');

    if (obj != 'null')
    {
        if (e.target.id != 'show_calender')
            obj.style.display='none';
    }
}

Error is in Internet Explorer: e.target.id is undefined.

like image 509
coderex Avatar asked Jul 17 '09 13:07

coderex


2 Answers

IE doesn't support the target property, they use srcElement instead.

Change:

if (e.target.id != 'show_calender')

to:

if ((e.target || e.srcElement).id != 'show_calender')

You may also need to add this to the beginning of your function:

if (!e) e = window.event

Your final code would look like this:

function check(e) { 
    if (!e) e = window.event;
    var obj = document.getElementById('calendar_widget');

    if (obj != 'null') {
        if ((e.target || e.srcElement).id != 'show_calender')
                obj.style.display='none';
    }
}
like image 68
Prestaul Avatar answered Oct 13 '22 23:10

Prestaul


Internet Explorer doesn't pass the event object to the event handler - it sets it as a property of the window object instead. Also, it uses srcElement instead of target. Try


document.click = check;

function check(e)
{ 
    var target = e ? e.target : window.event.srcElement;
    var obj = document.getElementById('calendar_widget');

    if (obj != 'null')
    {
        if (target.id != 'show_calender')
                obj.style.display='none';
    }
}

like image 32
NickFitz Avatar answered Oct 14 '22 00:10

NickFitz