Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery selector is not finding dynamically inserted input
    primarykey
    data
    text
    <p>I have a form with money value input fields, the values of which are added together on keyup event and the total inserted into another input field. this works fine.</p> <pre><code>&lt;table id="itemsqty"&gt;&lt;tr&gt;&lt;th style="min-width: 4em;"&gt;Qty&lt;/th&gt;&lt;th style="min-width: 10em;"&gt;Description&lt;/th&gt;&lt;th style="width: 4em;"&gt;Price&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;select name="qty[]" id="qty0"&gt;&lt;option selected="selected"&gt;1&lt;/option&gt; &lt;option&gt;2&lt;/option&gt;&lt;option&gt;3&lt;/option&gt;&lt;option&gt;4&lt;/option&gt;&lt;option&gt;5&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;td&gt; &lt;input placeholder="Enter item name in here" type="text" name="item[]" id="item0" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="itemprice" placeholder="Total price for this item" type="text" name="itemprice[]" id="itemprice0" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;select name="qty[]" id="qty1"&gt;&lt;option selected="selected"&gt;1&lt;/option&gt;&lt;option&gt;2&lt;/option&gt;&lt;option&gt;3&lt;/option&gt;&lt;option&gt;4&lt;/option&gt;&lt;option&gt;5&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;td&gt;&lt;input placeholder="Enter item name in here" type="text" name="item[]" id="item1" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input class="itemprice" placeholder="Total price for this item" type="text" name="itemprice[]" id="itemprice1" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;select name="qty[]" id="qty2"&gt;&lt;option selected="selected"&gt;1&lt;/option&gt; &lt;option&gt;2&lt;/option&gt;&lt;option&gt;3&lt;/option&gt;&lt;option&gt;4&lt;/option&gt;&lt;option&gt;5&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;td&gt; &lt;input placeholder="Enter item name in here" type="text" name="item[]" id="item2" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input class="itemprice" placeholder="Total price for this item" type="text" name="itemprice[]" id="itemprice2" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr id="totalrow"&gt;&lt;td&gt;&lt;a href="javascript:;" id="addanotherlnk"&gt;Add another item&lt;/a&gt;&lt;/td&gt;&lt;td style="text-align: right;padding-right: 7px;float: right;"&gt;&lt;label for="gtotal" &gt;Total:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input readonly type="text" name="gtotal" id="gtotal" value="£0.00" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre> <p>The value of the total input field (#gtotal) is updated using this script, and it works fine:</p> <pre><code>$('.itemprice').live("keyup", function (event) { var t = 0; for (var ip=0; ip &lt;3; ip++){ a = $('#itemprice'+ip).val(); a = a.replace("£",""); //t = t + a; t = Number(t) + Number(a); } //then add all values $('#gtotal').val(''); var formattedMoney = '£' + t.formatMoney(2,',','.'); $('#gtotal').val(formattedMoney); }); </code></pre> <p>Users can then add new fields using a link which fires this script:</p> <pre><code>$('#addanotherlnk').click(function(){ //count number of items of same class var ic; ic = $('.itemprice').length; $('&lt;tr&gt;&lt;td&gt;&lt;select name="qty[]" id="qty'+ic+'"&gt;&lt;option selected="selected"&gt;1&lt;/option&gt;&lt;option&gt;2&lt;/option&gt;&lt;option&gt;3&lt;/option&gt;&lt;option&gt;4&lt;/option&gt;&lt;option&gt;5&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;td&gt;&lt;input placeholder="Enter item name in here" type="text" name="item[]" id="item'+ic+'" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input class="itemprice" placeholder="Total price for this item" type="text" name="itemprice[]" id="itemprice'+ic+'" /&gt;&lt;/tr&gt;').insertBefore('#totalrow'); }); </code></pre> <p>This works fine and adds a unique ID to the inputs/ However, the scripts which updates the totals does not seem to recognise this new input field. I know it is something to do with the fact that this is a dynamically added field, hence why I used jquery .live() method but this did not fix the problem. </p> <p>All help is very much appreciated.</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.
    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