Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable enter submit

I have a form with a textfield inside and I am trying to disable the default behavior when the browser submits the whole form if the user presses Enter while the textfield is selected.

$('#recaptcha_response_field').keydown(function(event) { if (event.keyCode == 13) {
     event.preventDefault();
     event.stopPropagation();
     event.stopImmediatePropagation();
     alert("You Press ENTER key");
     return false;
   } 
});

Currently am getting "You Press ENTER key" and the default behavior isn't overridden.

like image 958
user2288298 Avatar asked Apr 17 '13 14:04

user2288298


People also ask

How do you disable enter submit?

To prevent this from happening you can simply stop the form being submitted if the enter key had been pressed. This is done by binding a JQuery event to the input elements of the form and returning false if the key pressed is enter, which has the keyCode value of 13.

How avoid enter key submit form React?

To prevent form submission when the Enter key is pressed in React, use the preventDefault() method on the event object, e.g. event. preventDefault() . The preventDefault method prevents the browser from refreshing the page when the form is submitted.


1 Answers

To prevent the script from blocking the enter key on other elements such as on a textarea. Change the target from "form" to "input".

$(document).on("keypress", "input", function (e) {
    var code = e.keyCode || e.which;
    if (code == 13) {
        e.preventDefault();
        return false;
    }
});
like image 188
Rob Powell Avatar answered Sep 30 '22 20:09

Rob Powell