Reputation: 27
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.
Upvotes: 3
Views: 860
Reputation: 11055
To be more reliable about @Jamiec's answer, I also added onMouseOut because when you start mouseDown (without any mouseUp) and move your mouse out of the trigger, the incrementar or decerementar won't stop anymore:
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)
}
a{
display:inline-block;
border:1px solid #aaaaaa;
padding:10px;
}
<input type="number" name="bodega" id="bodega" value="15">
<input type="number" name="sala" id="sala" value="5">
<a onMouseOut="endIncrementar()" onClick="incrementar()" onMouseDown="beginIncrementar()" onMouseUp="endIncrementar()">+</a>
<a onMouseOut="endDecrementar()" onClick="decrementar()" onMouseDown="beginDecrementar()" onMouseUp="endDecrementar()">-</a>
Upvotes: 0
Reputation: 9561
Use setInterval() and clearInterval()
on onmousedown
and onmouseup
events. Check below example.
var salainv = document.getElementById("sala").value;
var bodegainv = document.getElementById("bodega").value;
var timer;
function incrementar() {
document.getElementById("sala").value = salainv++;
document.getElementById("bodega").value = bodegainv--;
}
function decrementar() {
document.getElementById("sala").value = salainv--;
document.getElementById("bodega").value = bodegainv++;
}
function mouseDown(v) {
if (v == 'inc')
timer = setInterval(incrementar, 100);
else
timer = setInterval(decrementar, 100);
}
function mouseUp(v) {
clearInterval(timer);
timer = null;
}
<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="mouseDown('inc')" onmouseup="mouseUp('inc')">
<img src="http://images.clipartpanda.com/minus-clipart-RTA7dagTL.png" style="width: 40px;" onClick="decrementar()" onmousedown="mouseDown('dec')" onmouseup="mouseUp('dec')">
Upvotes: 0
Reputation: 136124
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()">
Upvotes: 4