Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type number: how to detect if value was incremented or decremented?

Up until now, I simply used "change" to see if an input field of the type "number" was changed. However, now I need to know if the number was incremented or decremented to perform different actions. How can I see how the number was changed?

Looking for solutions with JQuery, but plain old JavaScript is fine as well.

like image 496
noClue Avatar asked Jan 29 '23 05:01

noClue


1 Answers

You could simply previously store the value of your input and compare it on change :

let value = $('#test').val();

$('#test').on('change',function(){
  if($(this).val() > value){
    console.log('Input was incremented');
  }else{
    console.log('Input was decremented');
  }
  
  value = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="test" value="0">
like image 197
Zenoo Avatar answered Jan 31 '23 08:01

Zenoo