'Get the ENTER click event of input when type="number"

In the HTML input text, using the onKeyUp method I am able to catch the click event on enter button when input type="text" .

But the same event is not able to fire if the input type="number".

HTML

<input id="inputLocation" type="text" class="inputBarCode" style="text-transform: uppercase" placeholder: placeholder/>

JS

 $('#inputLocation').keyup(function (e) {
     if (e.which === 13) {
         $('#inputLocation').blur();
         //self.executeLocationLookup();
         alert("Event ", e.which);
     }
 });

Can you please let me know how to get the click event(13) of the enter button if input type is numeric keyboard

Please Refer the attached image



Solution 1:[1]

Use event.target.value property or $(this).val()

$('#inputLocation').keyup(function (e) {
  if (e.which === 13 && isNumber(e.target.value)) {
     $('#inputLocation').blur();
     //self.executeLocationLookup();
     alert("Event ", e.which);
  }
});  

function isNumber(n) {
   return !isNaN(parseFloat(n)) && isFinite(n);
}

Solution 2:[2]

It is better to actually listen to the submit event, which is triggered when the enter key is pressed.

Use the following markup:

<form>
    <input id="inputLocation" type="text" class="inputBarCode" style="text-transform: uppercase" placeholder: placeholder/>
</form>

and you can use submit() to listen for the submit event.

$('form').submit(function (e) {
    // prevent the page from auto-navigating on form submit
    e.preventDefault();

    // do something
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mihai Alexandru-Ionut
Solution 2 Daniel Apt