Alexander  Tolkachev Alexander Tolkachev - 5 months ago 21
jQuery Question

Monitoring input field change jquery

How to detect whent i increment/decrement input value? The following code does not work for me(Using jquery 1.8)



$(document).ready(function(){
$("input[id=count]").change(function(){
alert($(this).val());
});
});

<input type="number" id="count" value="1">




Answer

Use input event using .on method.

The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed.

change event on input type number/text is invoked when focus of the element is lost

Note: $("#count") is more appropriate selector(ID selector) than using Attribute Equals Selector [name=”value”] selector

$(document).ready(function() {
  $("#count").on('input', function() {
    console.log($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="number" id="count" value="1">

Comments