Skip to content

Instantly share code, notes, and snippets.

@bradonomics
Created November 15, 2017 12:38
Show Gist options
  • Select an option

  • Save bradonomics/daf35f5d333558f4b5fafc35256b006e to your computer and use it in GitHub Desktop.

Select an option

Save bradonomics/daf35f5d333558f4b5fafc35256b006e to your computer and use it in GitHub Desktop.
Increase and decrease input field value
//* Increase the occupancy in booking widget
$(".increment").click(function() {
var $increment = $(this);
//* Get the current input value
var originalValue = $increment.parent().siblings("input").val();
//* Increase the value by 1
var newValue = parseFloat(originalValue) + 1;
//* Apply the value to the input field
$increment.parent().siblings("input").val(newValue);
});
//* Decrease the occupancy in booking widget
$(".decrement").click(function() {
var $decrement = $(this);
//* Get the current input value
var originalValue = $decrement.parent().siblings("input").val();
//* Init variable outide the conditional
var newValue;
if (originalValue > 0) {
//* Decrease the value if it's greater than 0
newValue = parseFloat(originalValue) - 1;
} else {
//* You can't have negitive people Yo!
newValue = 0;
}
//* Apply the value to the input field
$decrement.parent().siblings("input").val(newValue);
});
<form action="#">
<fieldset class="arrival-holder">
<label>Arrival</label>
<input id="datepicker-arrival" type="text" name="arrival_date" aria-label="Arrival Date">
<i class="fa fa-angle-down" aria-hidden="true"></i>
</fieldset>
<fieldset class="departure-holder">
<label>Departure</label>
<input id="datepicker-departure" type="text" name="departure_date" aria-label="Departure Date">
<i class="fa fa-angle-down" aria-hidden="true"></i>
</fieldset>
<fieldset class="rooms-holder">
<label>Rooms</label>
<input type="text" name="room_quantity" value="1">
<div class="buttons">
<div class="decrement">-</div>
<div class="increment">+</div>
</div>
</fieldset>
<fieldset class="adults-holder">
<label>Adults</label>
<input type="text" name="adult_occupancy" value="2">
<div class="buttons">
<div class="decrement">-</div>
<div class="increment">+</div>
</div>
</fieldset>
<fieldset class="children-holder">
<label>Children</label>
<input type="text" name="child_occupancy" value="0">
<div class="buttons">
<div class="decrement">-</div>
<div class="increment">+</div>
</div>
</fieldset>
<input class="submit" type="submit" value="Check Availability">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment