Stop form submission on "enter" keypress

Permalink
I'm trying to write a block that uses uses an "enter" key press to trigger a JavaScript method. I've been trying stop the propagation of the event using JavaScript, but haven't been successful. No matter what I've tried the save action is triggered and the add/edit block window closes.

Does anyone know how to block the form submission on "enter" key press?

Thanks.

dzimney
 
dzimney replied on at Permalink Best Answer Reply
dzimney
Here is an example of the javascript I'm using to block the form submission:

$("input#my-text-field").bind("keyup", function(e) {
   var form = $("#ccm-block-form");
   var keyCode = e.which;
   if(keyCode == 13 ) {
      console.log("ENTER"); // this is firing on enter keypress
      form.submit(function() {return false;}); // attempt to kill form submission fails
   }
});
dzimney replied on at Permalink Reply
dzimney
Not sure what was going on, but I duplicated the same block into a different project, using the same JS as posted above and the blocking of the form submission worked as expected. Maybe it was just a long day.