I'm encountering a minor issue. I have a form containing a few inputs[type="text"]
wrapped in div elements (plus and minus). How can I increment and decrement these inputs without having to create variables, classes, or ids for each element? Someone suggested using arrays, but I'm unsure of how to approach this...
The current solution involves assigning classes to each tag...
var child = document.querySelector("#child");
var adult = document.querySelector("#adult");
var rooms = document.querySelector("#rooms");
var plusChild = document.querySelector(".plus_child");
var minusChild = document.querySelector(".minus_child");
var childV = child.value;
var adultV = adult.value;
var roomsV = rooms.value;
plusChild.addEventListener("click", function(){
if(childV<10){
childV++;
child.value = "" + childV;
}
});
minusChild.addEventListener("click", function(){
if(childV>0) {
childV--;
child.value = "" + childV;
}
});
.main-form {
width: 500px;
margin: 0 auto;
}
.input {
display: inline-block;
width: 100px;
height: 15px;
background: none;
outline: none;
}
.plus {
display: inline-block;
background: #999999;
width: 15px;
height: 15px;
}
.minus {
display: inline-block;
background: #999999;
width: 15px;
height: 15px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="" class="main-form">
<div class="input-item">
<span>child</span>
<div class="toggle plus plus_child">+</div>
<input type="text" class="input" placeholder="0" id="child">
<div class="toggle minus minus_child">-</div>
</div>
<br>
<div class="input-item">
<span>adult</span>
<div class="toggle plus">+</div>
<input type="text" class="input" placeholder="0" id="adult">
<div class="toggle minus">-</div>
</div>
<br>
<div class="input-item">
<span>rooms</span>
<div class="toggle plus">+</div>
<input type="text" class="input" placeholder="0" id="rooms">
<div class="toggle minus">-</div>
</div>
</form>
<script src="script.js"></script>
</body>
</html>