javascript - jQuery check if input value increases/decreases on change -
i have input type number
<input type="number" value="5" id="nmovimentos"/>
i want especific action when value increases or decreases (alert simpler example).
i have following jquery code:
$(document).ready(function(){ var oldvalue = $("#nmovimentos").val(); $("#nmovimentos").change(function(){ var newvalue = $(this).val(); if (newvalue > oldvalue) alert("increase!"); else alert("decrease!"); }); });
but doesn't work because can't detect oldvalue var.. clues on how that? thank much!
you can make use of property every htmlinputelement has store previous value, example defaultvalue
. in case save couple of lines of code, , make code little cleaner , concise:
$("#nmovimentos").change(function () { var direction = this.defaultvalue < this.value this.defaultvalue = this.value; if (direction) alert("increase!"); else alert("decrease!"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" value="5" id="nmovimentos" />
Comments
Post a Comment