This question has already been answered:
Hello!
When adding goods to the basket, the user has the opportunity to set an arbitrary number of goods before adding.
I need the following: by clicking on the "Add to cart" button of a specific product - to take from it the same quantity of goods that the user has entered.
In my case, the number of goods is always added to the same product, regardless of which product clicked. I check this with alert() . In the example, everything is shown. Look here please.
Please tell me how it is possible to solve this problem.
$('.add-to-cart').on('click', function(e) { var that = $(this), shows = parseInt(that.attr("data-show"), 10), add = that.attr("data-text"), qty = parseInt($('#qty').val(), 10); alert(qty); if (shows == 0) { e.preventDefault(); that.attr("data-show", "1"); that.text(add); that.closest('.catalog-tovarov').find('#mot').show(); } }); .catalog-tovarov { display: inline-block; vertical-align: top; text-align: center; margin: 0 0 20px 11px; width: 150px; height: 230px; padding: 1%; transition: 0.4s 0.2s ease; background: #cfcfcf; } .catalog-tovarov:hover { box-shadow: 0 4px 9px 0 #CECECE; } .price { margin-top: 120px; } #mot { display: none; text-align: right; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="catalog-tovarov"> <div id="mot"><img width="30px" src="http://sparkysite.ru/small/check/check03/scheck264.png" /></div> <div class="price"> <input id="qty" type="text" name="quant" value="3" /> <span>444 ΡΡΠ±.</span><br/> <a class="add-to-cart" data-price="10000" data-show="0" data-text="ΠΠ΅ΡΠ΅ΠΉΡΠΈ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ" href="#">Π ΠΊΠΎΡΠ·ΠΈΠ½Ρ</a> </div> </div> <div class="catalog-tovarov"> <div id="mot"> <img width="30px" src="http://sparkysite.ru/small/check/check03/scheck264.png" /> </div> <div class="price"> <input id="qty" type="text" name="quant" value="2" /> <span>444 ΡΡΠ±.</span><br/> <a class="add-to-cart" data-price="10000" data-show="0" data-text="ΠΠ΅ΡΠ΅ΠΉΡΠΈ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ" href="#">Π ΠΊΠΎΡΠ·ΠΈΠ½Ρ</a> </div> </div> <div class="catalog-tovarov"> <div id="mot"> <img width="30px" src="http://sparkysite.ru/small/check/check03/scheck264.png" /> </div> <div class="price"> <input id="qty" type="text" name="quant" value="6" /> <span>444 ΡΡΠ±.</span><br/> <a class="add-to-cart" data-price="10000" data-show="0" data-text="ΠΠ΅ΡΠ΅ΠΉΡΠΈ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ" href="#">Π ΠΊΠΎΡΠ·ΠΈΠ½Ρ</a> </div> </div>
iddifferent elements:qty = parseInt($(this).closest('.price').find('.qty').val(), 10);. Well:<input class="qty" ...Same forid="mot". - Igor<div class="price">to attach another div<div class="price"><div class="qwerty"><input class="qty" ...and then when referring toparseInt($(this).closest('.qwerty').find('.qty').val(), 10);does not work? - Dmitriy