Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make this jquery function more efficient?
    primarykey
    data
    text
    <p>I have a checkout I'm playing with which has some up and down arrows next to the quantity input of each item.</p> <p>I have some repeated code and it doesn't seem that efficient. Hopefully you can help.</p> <p>Each item looks something like this (please excuse my non-standard-conforming item_id attribute for now)</p> <pre><code>&lt;div class="qty_ctrl_wrap"&gt; &lt;input class="bag_qty" type="text" value="4" item_id="532" name="product_quantity"&gt; &lt;div class="arrows"&gt; &lt;div class="more"&gt;&lt;/div&gt; &lt;div class="less"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Here's the full jquery function:</p> <pre><code>$('#checkout .arrows div').live('click',function(){ var item_id = $(this).parent().prev('input').attr('item_id'); var current_qty = $(this).parent().prev('input').val(); if($(this).attr('class') == 'more') { var new_qty = parseInt(current_qty) + 1; } else { var new_qty = parseInt(current_qty) - 1; } // change the input on screen for visual reasons... $(this).parent().prev('input').val(new_qty); // submit the changes $.ajax({ type: 'POST', url: '/backend/product_purchase_items.php', data: 'qty='+new_qty+'&amp;item_id='+item_id, cache: false, success: function(data) { $('#product_purchase_items').html(data); } }); }); </code></pre> <p>As you can see, I'm repeating the following thing 3 times:</p> <pre><code>$(this).parent().prev('input') </code></pre> <p>I've been trying to work out how to put that in a variable so I don't have to repeat that code.</p> <p>But in addition to that, the way this works is that if you hit the arrow 3 times, it does <strong>3 seperate ajax posts</strong>. Ideally there would be a slight pause before sending the post.</p> <p>How does one make a pause of say 300 milliseconds to see if there are further clicks of the arrows before submitting the post?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload