Allison
Allison

Reputation: 88

Stopping enter key to click button

I have tried many ways i dont know what i miss now

<input id="stopBuy" class="grn" type="button" onclick="selectGT()" value="Buy Now">

Issue in this when we we click once on button and if mouse is over button and then we press enter it get clicked again. so i wanted to know if there is way to stop this?

I tried putting this in whole container but does not work. I have included jQuery v1.8.3

<script>
$('#container').keypress(function(e) {  // even try putting #stopBuy
    if(e.which == 13) { 
        e.preventDefault();
    }
});
</script>

Upvotes: 2

Views: 84

Answers (3)

TiiJ7
TiiJ7

Reputation: 3412

Blur the button after it is clicked:

$('#stopBuy').click( function() {
    $(this).blur();
});

Upvotes: 0

Akaryatrh
Akaryatrh

Reputation: 531

Maybe if you stop event propagation ?

$('#container').keypress(function(e) {  // even try putting #stopBuy
   if(e.which == 13) { 
      e.preventDefault();
      e.stopPropagation();
   }
});

Upvotes: 0

Felix
Felix

Reputation: 38112

Try to put your code inside DOM ready $(document).ready(function() { }); or $(function () { }) to make it work:

$(document).ready(function() {
    $('#container').keypress(function(e) {  // even try putting #stopBuy
        if(e.which == 13) { 
            e.preventDefault();
        }
   });
});

Upvotes: 4

Related Questions