I have an input box surrounded by add and subtract button. You can enter numbers in the input box manually or you can press the add or subtract button to increase or decrease the number
HTML
<button type="button" class="btn btn-info changeValue" onclick="decreaseValue()">-</button>
<input type="number" class="form-control" name="quantity" id="addSub" value="0" placeholder="Quantity" required>
<button type="button" class="btn btn-info changeValue" onclick="increaseValue()">+</button>
Javascript
function increaseValue() {
var value = parseInt(document.getElementById('addSub').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('addSub').value = value;
}
function decreaseValue() {
var value = parseInt(document.getElementById('addSub').value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('addSub').value = value;
}
Now I have button to generate this input box dynamically. Multiple input box can be generated. All the add and sub buttons in the input box will however target the original input box. Is there a way to make it so that the add and sub buttons will always target the input box right next to it?
.appendto create the inputbox and buttons on button click. There is not a way for me give a unique ID to each input box.