I have written the following Javascript code which increments and decrements 3 separate inputs by 1:
//INCREMENTS/DECREMENTS BEDROOMS BY 1
function plusOneBedrooms() {
var addRoom = document.getElementById('rooms_amount_bedroom');
value = parseInt(addRoom.getAttribute('value'), 10) + 1;
addRoom.setAttribute('value', value);
addRoom.innerHTML = value;
}
function minusOneBedrooms() {
var subtractRoom = document.getElementById('rooms_amount_bedroom');
value = parseInt(subtractRoom.getAttribute('value'), 10) - 1;
subtractRoom.setAttribute('value', value);
subtractRoom.innerHTML = value;
}
//INCREMENTS/DECREMENTS BATHROOMS BY 1
function plusOneBathrooms() {
var addRoom = document.getElementById('rooms_amount_bathrooms');
value = parseInt(addRoom.getAttribute('value'), 10) + 1;
addRoom.setAttribute('value', value);
addRoom.innerHTML = value;
}
function minusOneBathrooms() {
var subtractRoom = document.getElementById('rooms_amount_bathrooms');
value = parseInt(subtractRoom.getAttribute('value'), 10) - 1;
subtractRoom.setAttribute('value', value);
subtractRoom.innerHTML = value;
}
//INCREMENTS/DECREMENTS KITCHENS BY 1
function plusOneKitchens() {
var addRoom = document.getElementById('rooms_amount_kitchens');
value = parseInt(addRoom.getAttribute('value'), 10) + 1;
addRoom.setAttribute('value', value);
addRoom.innerHTML = value;
}
function minusOneKitchens() {
var subtractRoom = document.getElementById('rooms_amount_kitchens');
value = parseInt(subtractRoom.getAttribute('value'), 10) - 1;
subtractRoom.setAttribute('value', value);
subtractRoom.innerHTML = value;
}
<div class="room_type_wrap">
<small class="counter_title counter_opacity1">Bedrooms</small>
<hr class="counter_title_underline">
<div class="counter_wrap">
<div class="subtractRoom" onclick="minusOneBedrooms()">
<img src="./assets/images/arrow_down.png" alt="" class="counter_down_arrow">
</div>
<input type="number" class="room_count" id="rooms_amount_bedroom" autocomplete="off" value="0">
<div class="addRoom" onclick="plusOneBedrooms()">
<img src="./assets/images/arrow_up_white.png" alt="" class="counter_up_arrow">
</div>
</div>
<small class="counter_title counter_opacity2">Bathrooms</small>
<hr class="counter_title_underline">
<div class="counter_wrap">
<div class="subtractRoom" onclick="minusOneBathrooms()">
<img src="./assets/images/arrow_down.png" alt="" class="counter_down_arrow">
</div>
<input type="number" class="room_count" id="rooms_amount_bathrooms" autocomplete="off" value="0">
<div class="addRoom" onclick="plusOneBathrooms()">
<img src="./assets/images/arrow_up_white.png" alt="" class="counter_up_arrow">
</div>
</div>
<small class="counter_title counter_opacity3">Kitchens</small>
<hr class="counter_title_underline">
<div class="counter_wrap">
<div class="subtractRoom" onclick="minusOneKitchens()">
<img src="./assets/images/arrow_down.png" alt="" class="counter_down_arrow">
</div>
<input type="number" class="room_count" id="rooms_amount_kitchens" autocomplete="off" value="0">
<div class="addRoom" onclick="plusOneKitchens()">
<img src="./assets/images/arrow_up_white.png" alt="" class="counter_up_arrow">
</div>
</div>
</div>
I would like to be able to have the same functionality but feel I could have probably done this in a simpler way. I'm new to Javascript/JQuery so any suggestions will be appreciated. Thanks!
addRoom.innerHTML = valuelooks like a mistake. \$\endgroup\$type=number. I think his code is just broken, and it isn't clear what his code was meant to achieve. \$\endgroup\$inputvalue by 1 when theaddRoomandsubtractRoombuttons are clicked. The code works I'm just having trouble trying to simplify the method i've used. \$\endgroup\$