15

I have a button and the following javascript routine.

$("button").keydown( function(key) {
  switch(key.keyCode) {
  case 32: //space
    return false;
  }
} );

as I understood it, the return false; would stop the keypress from being processed. So $("button").click(); would not be called. For other keyCodes, this works as expected. For example, if I intercept40, which is the down button, the page is not scrolling.

I noticed this behavior in Firefox.

Why does the return false; does not stop the button click event on space? What does the javascript spec say about this?

node_man
  • 1,359
  • 4
  • 23
  • 50
johannes
  • 7,262
  • 5
  • 38
  • 57
  • 1
    does it actually say "return fale" in your real code? If so, that might be your problem. fale. – rmeador Oct 28 '09 at 18:55
  • In your code snippet you have "return fale;" -- is this the code as it exists? if so it's a plain typo. – artlung Oct 28 '09 at 18:56
  • No I my real code returns false. I actually use this code to intercept other buttons like down, and it works for them. – johannes Oct 28 '09 at 18:58
  • Did you try `keyup` like one person did suggest (looks like it was then deleted)? It did work in FF2. I'm surprised to see it gone. – Crescent Fresh Oct 29 '09 at 02:06
  • 1
    @crescentfresh: I might have scared them off by saying you didn't need to use keyup. I think I may have messed up. – Tim Down Oct 29 '09 at 09:11
  • I did try to stop it in `keydown` and it works. But this does not answer my question. I want to know why? – johannes Oct 29 '09 at 18:26

3 Answers3

22

Hope this answers your question:

<input type="button" value="Press" onkeydown="doOtherStuff(); return false;">

return false; successfully cancels an event across browsers if called at the end of an event handler attribute in the HTML. This behaviour is not formally specified anywhere as far as I know.

If you instead set an event via an event handler property on the DOM element (e.g. button.onkeydown = function(evt) {...}) or using addEventListener/attachEvent (e.g. button.addEventListener("keydown", function(evt) {...}, false)) then just returning false from that function does not work in every browser and you need to do the returnValue and preventDefault() stuff from my other answer. preventDefault is specified in the DOM 2 spec and is implemented by most mainstream modern browsers. returnValue is IE-specific.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
7

First, if you're detecting a printable character such as space, you would be better off with the keypress event. Secondly, the way to prevent the default action is to call preventDefault() on the event in non-IE browsers and set the event's returnValue property to false in IE.

var button = document.getElementById("button");
button.onkeypress = function(evt) {
   evt = evt || window.event;
   var charCode = evt.keyCode || evt.which;
   if (charCode == 32) {
       if (evt.preventDefault) {
           evt.preventDefault();
       } else {
           evt.returnValue = false;
       }
   }
};

I'm not a jQuery expert and I assume it takes care of obtaining the event for you:

$("button").keypress(function(evt) {
   var charCode = evt.keyCode || evt.which;
   if (charCode == 32) {
       if (evt.preventDefault) {
           evt.preventDefault();
       } else {
           evt.returnValue = false;
       }
   }
});
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • I tested it, and the `key.preventDefault();` changes nothing in firefox. `keydown` does detect keys, which produce printable characters, so whats wrong with `keydown`? – johannes Oct 28 '09 at 21:32
  • OK. What's the HTML that produces the button? Are you sure the keypress handler is being called? In the case of space, you're probably OK to use `keydown` and `keyCode` 32, but in general with printable characters using `keypress` abstracts away any potential difference between keyboards or other input devices by extracting the character that resulted from the keypress rather than a code corresponding to a key on an input device. – Tim Down Oct 28 '09 at 22:36
  • @Tim: the OPs original code sample is valid jQuery for cancelling the event. None of the usual cross-browser code switching is necessary in this case. – Crescent Fresh Oct 29 '09 at 02:05
  • @cresecentfresh: OK. Guess the fact I don't use jQuery is showing through. When someone posts a better answer I'll delete this. – Tim Down Oct 29 '09 at 15:20
0

You can just use one attribute in form to stop submit on enter.<form method="post" onkeydown="return false;">

as like: onkeydown="return false;" in form attribute

<form method="post" onkeydown="return false;"></form> 
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Amit kumar
  • 19
  • 3