Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@devluis
Created October 18, 2014 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devluis/d758f0c3acc291f371ca to your computer and use it in GitHub Desktop.
Save devluis/d758f0c3acc291f371ca to your computer and use it in GitHub Desktop.
Increase / Decrease value on input with Jquery
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/Javascript">
$(document).ready(function () {
$(function () {
$('.add').on('click',function(){
var $qty=$(this).closest('p').find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal)) {
$qty.val(currentVal + 100);
}
});
$('.minus').on('click',function(){
var $qty=$(this).closest('p').find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal) && currentVal > 100) {
$qty.val(currentVal - 100);
}
});
});
});
</script>
</head>
<body>
<p>
<img src="http://i.imgur.com/yOadS1c.png" id="minus1" width="20" height="20" class="minus"/>
<input id="qty1" type="text" value="1" class="qty"/>
<img id="add1" src="http://i.imgur.com/98cvZnj.png" width="20" height="20" class="add"/>
</p>
<p>
<img src="http://i.imgur.com/yOadS1c.png" id="minus2" width="20" height="20" class="minus"/>
<input id="qty2" type="text" value="1" class="qty"/>
<img id="add2" src="http://i.imgur.com/98cvZnj.png" width="20" height="20" class="add"/>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment