JQuery increment maximum
JQuery increment maximum
I am learning jquery and have been given a little problem to solve and I cannot find the solution so hoping you can help me.
I am creating a small increment calculator which i have pasted together from bits and pieces I found online.
The problem I have is that i dont want it to go past 10, so if you press plus on teh calculator you cannot go any further. Also I dont want it to go past zero if you press minus, so no -1, -2 etc.
How would I achieve this?
The code i have is below.
many thanks!
Hayden
Price per item:<input name="basicPrice" value="50" class="input-text">
<br />
<input type="button" value="-" class="minus">
<input name="quantity" id="quantity" type="number" value="1" class="input-text">
<input type="button" value="+" class="plus">
<br />Money must pay:
<span class="newprice">20</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
function calculate(){
var basicPrice = parseInt($(":input[name='basicPrice']").val());
var quantity = parseInt($(":input[name='quantity']").val());
var total = basicPrice * quantity;
$(".newprice").text(total);
}
function changeQuantity(num){
$(":input[name='quantity']").val( parseInt($(":input[name='quantity']").val())+num);
}
$().ready(function(){
calculate();
$(".minus").click(function(){
changeQuantity(-1);
calculate();
});
$(".plus").click(function(){
changeQuantity(1);
calculate();
});
$(":input[name='quantity']").keyup(function(e){
if (e.keyCode == 38) changeQuantity(1);
if (e.keyCode == 40) changeQuantity(-1);
calculate();
});
var quantity = document.getElementById("quantity");
quantity.addEventListener("input", function(e) {
calculate();
});
});</script>