Launch button event on press enter key on Mozilla Firefox

function CaptureKeys (e,btn) {
  var c //= document.layers ? evt.which: document.all ? event.keyCode : evt.keyCode 
  if(window.event) { 
    c = e.keyCode; // IE
  } else if(e.which) {
    c = e.which; // Netscape/Firefox/Opera
  }
      if (c == 13) {
        //return /enter key
        if (btn=="go") {
            if (document.getElementById("ctl00_ContentPlaceHolder1_btnGo")!=null) {
                document.getElementById("ctl00_ContentPlaceHolder1_btnGo").focus();
                return true;
            }
        } else {
            if (document.getElementById('ctl00_ContentPlaceHolder1_ImgFilter') != null) {
            //__doPostBack('ctl00_ContentPlaceHolder1_ImgFilter','');
            document.getElementById('ctl00_ContentPlaceHolder1_ImgFilter').focus();
            return true;
           }
       }
      return false;
     }
 }

      

This code works on IE7 but doesn't work on Mozilla Firefox. Please help me to trigger the button event on key press Enter.

0


source to share


6 answers


Firefox assumes that if you hit the enter key in any of the text fields, you want to submit the form — even if the fields are not part of the form, and even if the button is not of the submit type.

You must override Firefox's default behavior with preventDefault (). In the jQuery selector, place in the div that contains the textboxes you want to ignore the enter key - in your case, the "page" div. Instead of selecting the entire div, you can also specify the text boxes you want to ignore.



$('#page').keypress(function(e) {
    if(e.which == 13) { // Checks for the enter key
        e.preventDefault(); // Stops IE from triggering the button to be clicked
    }
});

      

+1


source


You can simply add the UseSubmitBehavior = "true" attribute value to your button to be triggered on the Enterkey.



0


source


You can also try this code:

theButton.click();

      

0


source


I don't know if anyone is checking this thread, but for future reference.

I had the same problem in FF and got the answer here:

http://www.webdeveloper.com/forum/showthread.php?t=108382

Good luck!

0


source


Using:

 __doPostBack('ctl00$ContentPlaceHolder1$btnGo',''); 

      

instead:

document.getElementById("ctl00_ContentPlaceHolder1_btnGo").focus(); 

      

0


source


You have a typo.

window.event

should be window.event

!

0


source







All Articles