2

What I am trying to achieve in the below code is when "0" has been entered into the input field it gets unfocused and other stuff triggers.

$(".val-0").keyup(function() {
  if ($(this).val() === 0) {
    $(this).blur();
    // Do other Stuffss
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input class="val-0" type="number">
dim
  • 97
  • 1
  • 9

2 Answers2

1

You are missing an opening quotation on the class name, for one thing.

For another, you are using === to compare which requires same data type (strict comparison). input.val() returns data of type string not integer.

Deeper explanation here.

You want to compare using $(this).val() == 0) or $(this).val() === '0')

$(".val-0").keyup(function() {
  if ($(this).val() == 0) {
    $(this).blur();
    // Do other Stuffss
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input class="val-0" type="number">
Community
  • 1
  • 1
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

Due to some reason you cannot attached keyup event to input type of number. I tried to put working example here. I hope it will help you.

$(".val-0").keyup(function(event) {
console.log(event.which + ", " + $(this).val());
  if ($(this).val() === 0) {
    $(this).blur();
    console.log($(this));
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input class="val-0" type="text" />
jagdish.narkar
  • 317
  • 1
  • 7