Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript event.target not working in mozilla

<script type ='javascript'>
    function fun(userID) {
        var btn = event.target; // error 'event' undefine in mozilla 
        alert(btn.id);
    }
</script>

<asp:linkButton id ="target" style =" cursor:pointer" onclick ="fun('1')" >click here </asp:LinkButton>

I am new in JavaScript, I have written above code and this code is working fine in Google chrome but not working in Mozilla Firefox. can anyone suggest how to find control firing event?

like image 959
Durgesh Avatar asked Dec 21 '22 11:12

Durgesh


1 Answers

Pass event to the function:

<asp:linkButton id ="target" style =" cursor:pointer" onclick ="fun(event, '1')" >click here </asp:LinkButton>

function fun(event, userID)
{
    event= event|| window.event;
    var btn = event.target; 
    alert(btn.id);
}

OR

Make sure your event is not undefined

function fun(userID)
{
    var e = event || window.event;
    var btn = e.target; 
    alert(btn.id);
}
like image 138
Akhil Sekharan Avatar answered Jan 02 '23 23:01

Akhil Sekharan