benhowdle89
benhowdle89

Reputation: 37504

Cancel form submit but allow enter key event

I'm trying to disable Enter key submit on a form but allow the Enter key event to be registered for some other code I have listening. Is this possible?

$('#createPost :input').on("keypress", function(e) {

    if(e.target.id == 'post-author' && e.which == 13){
        e.stopPropagation();
        e.stopImmediatePropagation();
    }
});

Is not working :( it still submits the form

Upvotes: 0

Views: 621

Answers (1)

kirilloid
kirilloid

Reputation: 14304

You need to call e.preventDefault() to cancel submit action.

return false; from handler also works. And that works in more ancient browsers, even though this is not important with jQuery.

Upvotes: 1

Related Questions