There is input[text] and two buttons "+" and "-", which change the value in input to +1 or -1. It is required to intercept an event when the value is greater than a certain value, for example, greater than 10 and execute a function.

How can this be done using JS / jQuery?

2 answers 2

Create a checkCount function checkCount , when accessed in real time, checks the value of the input and does something inside. When the plus and minus refer to it.

PS The code also provides a manual input option - then there is also a link to the checkCount function.

 $(document).on('click', '#plus', function(){ var count = Number($('#count').val()); $('#count').val(count+1); checkCount(); }); $(document).on('click', '#minus', function(){ var count = Number($('#count').val()); $('#count').val(count-1); checkCount(); }); $(document).on('input', '#count', checkCount); function checkCount() { var count = Number($('#count').val()); if(count > 10) { alert('Больше десяти'); } else if(count < 0) { alert('Меньше нуля'); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="minus">-</button> <input type="text" id="count" /> <button id="plus">+</button> 

     $(".target").on("input",function() { console.log($(".target").val()); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="target" type="text" placeholder="введите что-нибудь">