javascript - How to individually increase or decrease a value using jquery -
i looking increase or decrease each products clicking + or - button individually, issues when click + button.. products value increased instead of one, same when click - button. kindly me out how this
jquery
$(document).ready( function(){ $(".quantity-adder .add-action").click( function(){ if( $(this).hasclass('add-up') ) { $("[name=quantity]",'.quantity-adder').val( parseint($("[name=quantity]",'.quantity-adder').val()) + 1 ); }else { if( parseint($("[name=quantity]",'.quantity-adder').val()) > 1 ) { $("input",'.quantity-adder').val( parseint($("[name=quantity]",'.quantity-adder').val()) - 1 ); } } } ); });
html
<div class="product-extra"> <p>product 1</p> <div class="quantity-adder pull-left"> <div class="quantity-number pull-left"> <span>qutantity</span> <input type="text" name="quantity" size="2" value="1" /> </div> <div class="quantity-wrapper pull-left"> <span class="add-up add-action fa fa-plus"></span> <span class="add-down add-action fa fa-minus"></span> </div> <input type="hidden" name="product_id" size="2" value="40" /> </div> </div> <br /> <br /> <br /> <div class="product-extra"> <p>product 2</p> <div class="quantity-adder pull-left"> <div class="quantity-number pull-left"> <span>qutantity</span> <input type="text" name="quantity" size="2" value="1" /> </div> <div class="quantity-wrapper pull-left"> <span class="add-up add-action fa fa-plus"></span> <span class="add-down add-action fa fa-minus"></span> </div> <input type="hidden" name="product_id" size="2" value="44" /> </div> </div> <br /> <br /> <br /> <div class="product-extra"> <p>product 3</p> <div class="quantity-adder pull-left"> <div class="quantity-number pull-left"> <span>qutantity</span> <input type="text" name="quantity" size="2" value="1" /> </div> <div class="quantity-wrapper pull-left"> <span class="add-up add-action fa fa-plus"></span> <span class="add-down add-action fa fa-minus"></span> </div> <input type="hidden" name="product_id" size="2" value="48" /> </div> </div> <br /> <br /> <br /> <div class="product-extra"> <p>product 4</p> <div class="quantity-adder pull-left"> <div class="quantity-number pull-left"> <span>qutantity</span> <input type="text" name="quantity" size="2" value="1" /> </div> <div class="quantity-wrapper pull-left"> <span class="add-up add-action fa fa-plus"></span> <span class="add-down add-action fa fa-minus"></span> </div> <input type="hidden" name="product_id" size="2" value="55" /> </div> </div> <br /> <br /> <br />
demo link https://jsfiddle.net/sjyq64mj/
$(document).ready(function () { $(".quantity-adder .add-action").click(function () { if ($(this).hasclass('add-up')) { var text = $(this).parent().parent().parent().find("[name=quantity]", '.quantity-adder') text.val(parseint(text.val()) + 1); } else { var text = $(this).parent().parent().parent().find("[name=quantity]", '.quantity-adder') if (parseint(text.val()) > 1) { text.val(parseint(text.val()) - 1); } } }); });
i added .parent()
find proper text increase
Comments
Post a Comment