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?

1
  • Here is the detailed answer related to it. Click here Commented Apr 18, 2017 at 11:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the rply but again same problem is there.
I currently run Firefox 10.0.2
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
what is this problem? Can you be a bit more specific? What do you get in the alert?
ok, but what exactly is the problem? Is the alert showing properly but the element not being updated?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.