-1

I have an asp:TextBox on my page and I would like to detect the enter key to call a javascript function. This is what I have:

Codebehind

txtSearch.Attributes.Add("OnKeyPress", "ProcessKeyPressed()")

aspx page

 function ProcessKeyPressed() {
    if (event.keyCode == 13 || event.keyCode == 10 ) {
        Search();
    }
 }

This works in chrome and IE, but not in Firefox...any idea why this might be the case?

Thanks in advance,

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
user1839862
  • 13
  • 1
  • 4
  • Does not work how? Does the ProcessKeyPressed function not get called at all? Is there any error? Have you used the Firefox JavaScript console or Firebug to check for errors? – scott.korin Mar 15 '13 at 10:25
  • possible duplicate of [onKeyPress event not working in Firefox](http://stackoverflow.com/questions/4496910/onkeypress-event-not-working-in-firefox) (you should check the second answer) – tpeczek Mar 15 '13 at 10:25
  • Yep you're right tpeczek, my question is for that same issue. Thanks for the help. – user1839862 Mar 17 '13 at 16:03

3 Answers3

2

Try:

function ProcessKeyPressed(event) {
   if (event.keyCode == 13 || event.keyCode == 10 ) {
       Search();
   }
}

See here for more info: window.event.keyCode how to do it on Firefox?

Community
  • 1
  • 1
Brandon
  • 16,382
  • 12
  • 55
  • 88
  • I tried the same but not working on custom control textbox my code is----- and Function on aspx ... function ProcessKeyPressed(event) { if (event.keyCode == 13 || event.keyCode == 10 ) { Search(); } } – Shailendra Mishra Jul 20 '17 at 10:47
1

Code behind:

txtSearch.Attributes.Add("OnKeyPress", "ProcessKeyPressed(event || window.event)")

And JS:

function ProcessKeyPressed(event) {
    if (event.keyCode == 13 || event.keyCode == 10 ) {
        Search();
    }
 }

Based on this

Demo

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
0

You may try this

function ProcessKeyPressed(e) {
    var event = e || window.event;
    var key = event.charCode || event.keyCode
    if (key == 13 || key == 10 ) {
        Search();
    }
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307