raina
raina

Reputation: 91

Keycodes not working in Mozilla with Javascript

I have code in javascript which get keycodes of different keys and set it to hidden field. Hidden field is then manipulated by server side code. My code is:

function TriggeredKey(e) {
   e = e || window.event;
   var keycode;
   if (window.event){
     keycode = event.which ? window.event.which : window.event.keyCode;
   }
   alert(keycode);
    document.getElementById("<%=hdfkey.ClientID %>").value = keycode;
   _dopostback();
}

This code works fine in Chrome but not in Mozilla. Can someone please provide me the solution for this problem?

Upvotes: 1

Views: 4281

Answers (1)

Jibi Abraham
Jibi Abraham

Reputation: 4696

function TriggeredKey(e) {
   e = e || window.event;
   var keycode;
   if (window.event){
       //this check fails in mozilla/
       //so the variable keycode is undefined
       keycode = event.which ? window.event.which : window.event.keyCode;
   }
   if(!keycode){keycode = e.which}
   //solves the issue
   alert(keycode);
   document.getElementById("<%=hdfkey.ClientID %>").value = keycode;
  _dopostback();
}

Fiddle

Check the fiddle in mozilla

Fiddle result

Fiddle result

Upvotes: 2

Related Questions