June 24, 2022

Make increment decrement with input filed in javascript

<button class="minus">minus</button>
<label><input type="text" value="0" id="inputno" /></label>
<button class="plus">plus</button>

 

function decrement(){
  var inputval = document.getElementById('inputno');	 
  //console.log(inputval)
  if(inputval.value <= 0 ){
    inputval.value = 0;	
    
  }else {
    inputval.value = parseInt(inputval.value) - 1;
    /* start set limit */
    inputval.style.background = 'white';
  inputval.style.color = 'black';
  /* end set limit */
    //alert(inputval.value);
  };	
};

function inrement () {

let incement = document.getElementById('inputno');

if(incement.value >= 5 ) {
  incement.value = 5;	
  //alert('Max 5');
  /* start set limit */
  incement.style.background = 'red';
  incement.style.color = 'white';
  /* end set limit */
  
}else {
   incement.value = parseInt(incement.value)+1;
}

}	

$(document).ready(function(){
$('.minus').on('click', function(){
  decrement();
});
$('.plus').on('click', function(){
  inrement();
});
   
})

 

Leave a Reply

Your email address will not be published. Required fields are marked *