Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery performing calculation on dynamically generated elements
    primarykey
    data
    text
    <p>I have a table made up of a row of 3 input elements: Price, Quanity, and Total. Under that, I have two links I can click to dynamically generate another row in the table. All that is working well, but actually calculating a value for the total element is giving me trouble. I know how to calculate the value of the first total element but I'm having trouble extending this functionality when I add a new row to the table. Here's the html:</p> <pre><code>&lt;table id="table" border="0"&gt; &lt;thead&gt; &lt;th&gt;Price&lt;/th&gt;&lt;th&gt;Quantity&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;input id ="price" type = "text"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input id ="quantity" type = "text"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input id ="total" type = "text"&gt;&lt;/input&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a href="#" id="add"&gt;Add New&lt;/a&gt; &lt;a href="#" id="remove"&gt;Remove&lt;/a&gt; </code></pre> <p>Here's the jquery I'm using:</p> <pre><code>$(function(){ $('a#add').click(function(){ $('#table &gt; tbody').append('&lt;tr&gt;&lt;td&gt;&lt;input id ="price" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;td&gt;&lt;input id ="quantity" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;td&gt;&lt;input id ="total" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;'); }); $('a#remove').click(function(){ $('#table &gt; tbody &gt; tr:last').remove(); }); }); $(function(){ $('a#calc').click(function(){ var q = $('input#quantity').val(); var p = $('input#price').val(); var tot = (q*p); $('input#total').val(tot); }); }); </code></pre> <p>I'm new to jquery so there's probably a simple method I don't know about that selects the relevant fields I want to calculate. Any guidance would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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