javascript - how to set the limit of minimum and maximum value in Jquery or js -


i have script increment decrements value of text box, it's working fine. thing need achieve is, have set minimum , maximum value decrements , increments receptively.

i don't need alert n message show user, need should not change value after reaching limit.

  <script>     $(document).ready(function(){   $("#up").on('click',function(){     $("#incdec input").val(parseint($("#incdec input").val())+1);    });     $("#down").on('click',function(){     $("#incdec input").val(parseint($("#incdec input").val())-1);    });   });  </script> 

demo

you can use following. set max , min value in data-max , data-min attribute in up , down buttons;

<input type="button" id="up" value="up" data-max="5"/> <input type="button" id="down" value="down" data-min="0"/> 

and in js;

$(document).ready(function() {     $("#up").on('click',function(){           if ($("#incdec").val() < $(this).data("max")) {             $("#incdec").val(parseint($("#incdec").val())+1);           }     });      $("#down").on('click',function(){           if ($("#incdec").val() > $(this).data("min")) {             $("#incdec").val(parseint($("#incdec").val())-1);           }     }); }); 

edit: image buttons : demo 2

edit2: how if creating buttons dynamically more once?

let have generating buttons dynamically , generate output below;

<div>     <input type="text" class="incdec" value="0"/>     <input type="button" class="up" value="up" data-max="5"/>     <input type="button" class="down" value="down" data-min="0"/> </div>  <div>     <input type="text" class="incdec" value="0"/>     <input type="button" class="up" value="up" data-max="5"/>     <input type="button" class="down" value="down" data-min="0"/> </div>  .... 

and can use following js:

$(document).ready(function() {     $(".up").on('click',function(){           var $incdec = $(this).parent().find(".incdec");           if ($incdec.val() < $(this).data("max")) {             $incdec.val(parseint($incdec.val())+1);           }     });      $(".down").on('click',function(){           var $incdec = $(this).parent().find(".incdec");           if ($incdec.val() > $(this).data("min")) {             $incdec.val(parseint($incdec.val())-1);           }     }); }); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -