Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot have multiple elements that share the same ID. An ID has to be unique throughout the whole document.</p> <p>That said, you have to change your code to use classes:</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 class="price" type = "text"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="quantity" type = "text"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="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>And:</p> <pre><code>$(function(){ $('a#add').click(function(){ $('#table &gt; tbody').append('&lt;tr&gt;&lt;td&gt;&lt;input class ="price" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;td&gt;&lt;input class ="quantity" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;td&gt;&lt;input class ="total" type = "text"&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;'); }); $('a#remove').click(function(){ $('#table &gt; tbody &gt; tr:last').remove(); }); }); </code></pre> <p>In order to provide a complete solution, where is your link with ID <code>calc</code>. How should the calculation work? Should every row has its own <code>calculation</code> button or do you have one global button the calculates the values for every row?</p> <p>If the latter you can use something like this:</p> <pre><code>$('a#calc').click(function(){ $('#table tr').each(function() { $(this).find('.total').val( parseFloat($(this).find('.quantity').val())* parseFloat($(this).find('.price').val()) ); } } </code></pre> <p><strong>Explanation:</strong></p> <p>When you click the link, the function loops over every row in the table, searches for the to input fields with class <code>quantity</code> and <code>price</code> and puts the result into the input with class <code>total</code> in that row.</p> <p>Note that I use <code>parseFloat</code> to make sure to use numbers for the calculation.</p> <hr> <p>Also note that you don't have to use <code>$(function(){})</code> around every single piece of JS code you create. Put <strong>everything</strong> that needs to be executed <strong>after the DOM</strong> loaded (because you access elements from your code) into this function. So you should do:</p> <pre><code>$(function() { $('a#add').click(function(){ // ..... }: $('a#calc').click(function(){ // ... }; } </code></pre> <hr> <p><strong>Update:</strong> If you want to use the <code>.blur()</code> method, here is an example:</p> <pre><code>$('#table').delegate('input.total' , 'blur', function(){ var row = $(this).closest('tr'); $(this).val( $('.quantity', row).val())* $('.price', row).val()) ); }); </code></pre> <p><a href="http://api.jquery.com/closest/" rel="nofollow noreferrer"><code>.closest()</code></a> gets the closest parent that matches the selector (in this case the row) and the rest is similar to the first function I wrote (getting the values of the particular row.</p> <p>I use the newer method <a href="http://api.jquery.com/delegate/" rel="nofollow noreferrer"><code>delegate()</code></a> here, but you can also use <a href="http://api.jquery.com/live/" rel="nofollow noreferrer"><code>.live()</code></a>. Note that you have to use one of them because your are creating the row dynamically and at the time e.g. <code>$(input.total).blur(function(){})</code> would be run (when the DOM is loaded) no row is created yet.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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