Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unless you are using a <em>really</em> old version of jQuery you should not use the <code>.live()</code> method at all. If using version 1.7 or later use the delegated form of <code>.on()</code>; if using older than 1.7 but newer than 1.4.2 use <code>.delegate()</code>.</p> <pre><code>$(document).on("click", ".productSubmitInput", function() { }) </code></pre> <p>is equivalent to</p> <pre><code>$(".productSubmitInput").live(function() { }) </code></pre> <p>...except better because it is more efficient and more flexible. Ideally instead of <code>$(document)</code> you'd do <code>$("#someForm")</code> where <code>"#someForm"</code> is the form that your buttons are in.</p> <p>Having said that, following is one way to convert your code. I'll assume that the arguments you pass to the <code>AddToCart()</code> function are specific to each button, so you'd still need to associate them with the button somehow - I'll use <code>data-</code> attributes:</p> <pre><code>&lt;input class="productSubmitInput" name="AddToCart_Submit" type="submit" value="Add to Cart" data-param1="26483" data-param2="3123041" data-param3="" data-param4="4" data-param5="false"&gt; </code></pre> <p>Then, having removed the inline <code>onclick=</code> attribute you'd do the following in a script block:</p> <pre><code>$(document).ready(function() { // $(document).on("click", ".productSubmitInput", function() { // OR, better $("#yourFormIdHere").on("click", ".productSubmitInput", function() { var $this = $(this); AddToCart( $this.data("param1"), $this.data("param2"), $this.data("param3"), $this.data("param4"), $this.data("param5") ); return false; }); }); </code></pre> <p>Of course if some of those <code>AddToCart()</code> parameters are the same for all buttons then you can put them directly into the JS rather than storing them as attributes in the HTML, for example if the last parameter is always <code>false</code>:</p> <pre><code> AddToCart( $this.data("param1"), $this.data("param2"), $this.data("param3"), $this.data("param4"), false ); </code></pre>
 

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