Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute js code on enter key form submit without form.submit?

I have a third party form to track users on the page. I need to track all page submits on the page including submit fails due to js validations. Broad structure is as following:

<form>
//Elements
</form>
<a id="btnSubmit"></a>

I do not have control over structure of the form. To track the form submits I am using anchor tag click event: $('#btnSubmit').click(function () { //my code }); I am not using form.submit as the js code used in form validation prevents form.submit event from being called. It works well with click event except when user submits form with enter key. How can I track enter key form submit without form.submit?

like image 272
Ruchit Rami Avatar asked Nov 22 '25 02:11

Ruchit Rami


1 Answers

You could try listening for the enter key being pressed on one of the input fields, or whatever suits in your form

$('input').keypress(function(e) {
        if(e.which == 13) {
            //your submit code
        }
    });
like image 123
andy mccullough Avatar answered Nov 23 '25 16:11

andy mccullough