1

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?

JohnP
  • 49,507
  • 13
  • 108
  • 140
raina
  • 81
  • 1
  • 3
  • 8

1 Answers1

2
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

Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
  • Thanks for the rply but again same problem is there. – raina Mar 23 '12 at 06:19
  • I currently run Firefox 10.0.2 – Jibi Abraham Mar 23 '12 at 06:23
  • yes, I have used this code on keydown event of a textbox in asp.net. In fiddle it is shown on keypress. same prblm in keydown event – raina Mar 23 '12 at 06:24
  • what is this problem? Can you be a bit more specific? What do you get in the alert? – Jibi Abraham Mar 23 '12 at 06:27
  • ok, but what exactly is the problem? Is the alert showing properly but the element not being updated? – Jibi Abraham Mar 23 '12 at 06:29
  • In chrome i get the keycode of that key but in mozilla it always show undefined for whatever key is pressed. i want that keycode in the hiddenfield to manipulate it at server side code but because of undefined keycode its not working at all in mozilla – raina Mar 23 '12 at 06:30
  • update the code you've posted here with the changes you've made – Jibi Abraham Mar 23 '12 at 06:32
  • function TriggeredKey(e) { e = e || window.event; var keycode; if (window.event) { keycode = event.which ? window.event.which : window.event.keyCode; } if (!keycode) { keycode = e.which } alert(keycode); _dopostback(); } – raina Mar 23 '12 at 06:35
  • This code is working in fiddle but when i m implementing this in my code its not working – raina Mar 23 '12 at 06:39
  • well I meant you to update the question, but no matter. The code you posted also works for me. I'm sorry, but I can't figure out any issues – Jibi Abraham Mar 23 '12 at 06:40