Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
How can I add rows to a table with this structure using javascript?

Basically I want to be able to click a button and have a new row created within the table for each of the columns used below. So for instance, it could be name: new book, quantity:30 etc.

HTML
<table id="dynamictable">
              <tbody>
                <tr>
                  <td class="name">My Favorite book</td>
                  <td class="quantity">x 20</td>
                  <td class="unit_price">£8.99</td>
                  <td class="total">£179.80</td>
                  <td class="remove"><img src="image/cross.png"></td>
                </tr>
               </tbody>
            </table>
Posted
Updated 4-Mar-13 4:00am
v2

Using jQuery you can easily add rows like below
JavaScript
$('#buttonId').click(function()
{
string htmlRow="<tr><td class="name">My Favorite book2</td></tr></tbody></table><table><tbody><tr><td class="quantity">x 20</td></tr></tbody></table><table><tbody><tr><td class="unit_price">£8.99</td></tr></tbody></table><table><tbody><tr><td class="total">£179.80</td></tr></tbody></table><table><tbody><tr><td class="remove"><img src="image/cross.png"></img></td></tr>";
$('#dynamictable>tbody').append(htmlRow);
}


Hope this helps
 
Share this answer
 
v2
Comments
Andrews Smith 4-Mar-13 12:09pm    
That wouldn't work. Just came up with this instead...



My Favorite book x 20 £8.99 £179.80 $('#buttonId').click(function() { string htmlRow="My Favorite book2
x 20
£8.99
£179.80
"; $('#dynamictable>tbody').append(htmlRow); }
Andrews Smith 4-Mar-13 12:09pm    
My code looks like this using your example above:

<table id="dynamictable">
<tbody>
<tr>
<td class="name">My Favorite book</td>
<td class="quantity">x 20</td>
<td class="unit_price">£8.99</td>
<td class="total">£179.80</td>
<td class="remove">
<img src="image/cross.png">
</td>
</tr>
</tbody>
</table>

$('#buttonId').click(function()
{
string htmlRow="<tr><td class="name">My Favorite book2</td></tr><table><tbody><tr><td class="quantity">x 20</td></tr></tbody></table><table><tbody><tr><td class="unit_price">£8.99</td></tr></tbody></table><table><tbody><tr><td class="total">£179.80</td></tr></tbody></table><table><tbody><tr><td class="remove"><img src="image/cross.png"></img></td></tr>";
$('#dynamictable>tbody').append(htmlRow);
}
The strategy is to first get the element ID you want to append to, then create a new row element with the new data, and then append that element to the ID you got in step 1.

Perhaps something like this?

XML
<pre lang="Javascript">var tableBody = document.getElementById('tableBody');
var newRow= document.createElement('tr');
newdiv.innerHTML = 'My Second Favorite Book';
tableBody.appendChild(newRow);</pre>



You'll need to give the tbody an id of 'tableBody'. You'll also then need to follow this strategy to add the td elements.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900