When mouse click and hold in the element plus, i want to increase or decrease the values, it is working but don't work on mouse hold
var salainv = document.getElementById("sala").value;
var bodegainv = document.getElementById("bodega").value;
function incrementar() {
document.getElementById("sala").value = salainv++;
document.getElementById("bodega").value = bodegainv--;
}
function decrementar() {
document.getElementById("sala").value = salainv--;
document.getElementById("bodega").value = bodegainv++;
}
<input type="number" name="bodega" id="bodega" value="15">
<input type="number" name="sala" id="sala" value="5">
<img src="https://cdn.pixabay.com/photo/2012/04/13/00/21/plus-31216_960_720.png" style="width: 40px;" onClick="incrementar()">
<img src="http://images.clipartpanda.com/minus-clipart-RTA7dagTL.png" style="width: 40px;" onClick="decrementar()">
Can somebody help me to make it work when mouse is pressed?
Thanks.
You can use setInterval as demonstrated below along with onMouseDown and onMouseUp events
var salainv = document.getElementById("sala").value;
var bodegainv = document.getElementById("bodega").value;
var timerId;
function incrementar() {
document.getElementById("sala").value = salainv++;
document.getElementById("bodega").value = bodegainv--;
}
function beginIncrementar(){
timerId = setInterval(incrementar,200);
}
function endIncrementar(){
clearInterval(timerId)
}
function decrementar() {
document.getElementById("sala").value = salainv--;
document.getElementById("bodega").value = bodegainv++;
}
function beginDecrementar(){
timerId = setInterval(decrementar,200);
}
function endDecrementar(){
clearInterval(timerId)
}
<input type="number" name="bodega" id="bodega" value="15">
<input type="number" name="sala" id="sala" value="5">
<img src="https://cdn.pixabay.com/photo/2012/04/13/00/21/plus-31216_960_720.png" style="width: 40px;" onClick="incrementar()" onMouseDown="beginIncrementar()" onMouseUp="endIncrementar()">
<img src="http://images.clipartpanda.com/minus-clipart-RTA7dagTL.png" style="width: 40px;" onClick="decrementar()" onMouseDown="beginDecrementar()" onMouseUp="endDecrementar()">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With