Skip to content

Instantly share code, notes, and snippets.

@Jhongt796
Created December 1, 2021 16:48
Show Gist options
  • Save Jhongt796/721727ea211ed0a2b7f38a2f72fca4b3 to your computer and use it in GitHub Desktop.
Save Jhongt796/721727ea211ed0a2b7f38a2f72fca4b3 to your computer and use it in GitHub Desktop.
Quantity selector
<section class="container">
<div class="product-quantity">
<h3>Quantity</h3>
<input data-min="1" data-max="0" type="text" name="quantity" value="1" readonly="true"><div class="quantity-selectors-container">
<div class="quantity-selectors">
<button type="button" class="increment-quantity" aria-label="Add one" data-direction="1"><span>&#43;</span></button>
<button type="button" class="decrement-quantity" aria-label="Subtract one" data-direction="-1" disabled="disabled"><span>&#8722;</span></button>
</div>
</div>
</div>
</section>
$("button").on("click", function(ev) {
var currentQty = $('input[name="quantity"]').val();
var qtyDirection = $(this).data("direction");
var newQty = 0;
if (qtyDirection == "1") {
newQty = parseInt(currentQty) + 1;
}
else if (qtyDirection == "-1") {
newQty = parseInt(currentQty) - 1;
}
// make decrement disabled at 1
if (newQty == 1) {
$(".decrement-quantity").attr("disabled", "disabled");
}
// remove disabled attribute on subtract
if (newQty > 1) {
$(".decrement-quantity").removeAttr("disabled");
}
if (newQty > 0) {
newQty = newQty.toString();
$('input[name="quantity"]').val(newQty);
}
else {
$('input[name="quantity"]').val("1");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
html {
font-size: 16px;
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
.container {
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
h3 {
display: block;
font-size: 0.6rem;
font-family: sans-serif;
text-transform: uppercase;
letter-spacing: 0.05rem;
margin-bottom: 0.25rem;
color: #333;
}
.product-quantity {
width: 10rem;
display: block;
}
input {
max-width: 2rem;
margin: 0;
padding: 0.75rem 0;
text-align: center;
border-top: 1px solid grey;
border-left: 1px solid grey;
border-bottom: 1px solid grey;
border-right: 0;
}
.quantity-selectors-container {
display: inline-block;
vertical-align: top;
margin: 0;
padding: 0;
}
.quantity-selectors {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
.quantity-selectors button {
-webkit-appearance: none;
appearance: none;
margin: 0;
border-radius: 0;
border: 1px solid grey;
background-color: #fff;
color: grey;
}
.quantity-selectors button:first-child {
border-bottom: 0;
}
.quantity-selectors button:hover {
cursor: pointer;
}
.quantity-selectors button[disabled="disabled"] {
cursor: not-allowed;
}
.quantity-selectors button[disabled="disabled"] span {
opacity: 0.5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment