RemovedQuasar
RemovedQuasar

Reputation: 29

Submit button fires twice

I have the following submit:

 <input type="submit"  id="buttonNext" name="buttoncrd"  value="Prosegui" class="buttonNavProsegui block-ui"/>

When you are on the page, i want to active the submit with the keyboard button "Entry" so i made this simple js code:

$(document).ready(function() {
    $(document).keypress(function(event){
       if (event.which == 13){
           $("#buttonNext").click();
        }
    });
});

It works correctly but i met some problem when i have the focus on the submit AND i press Enter on the keyboard. I fear that the submit fires twice, can you help me disable one submit when is focused?

Upvotes: 1

Views: 495

Answers (2)

Toben
Toben

Reputation: 111

You can use

 $(document).ready(function() {
    $(document).keypress(function(event){
       if (event.which == 13){
           $("#buttonNext").click();
           event.preventDefault();
        }
    });
});

It will not trigger twice.

Upvotes: 1

chautelly
chautelly

Reputation: 467

$("#buttonNext").addEventListener('keypress', e => {
  e.stopPropagation()
})

might do the trick. It prevents the event from propagating up in the hierarchy when a keypress event happens on the button.

Upvotes: 0

Related Questions