I’m inserting the row dynamically. How can I assign the id to each td so I can access it and update it after time. I’m sticking in it
$(document).ready(function(){
var rowNumber = 0;
var myData=new Array(6);
myData[0]="txt1";
myData[1]="txt2";
myData[2]="txt3";
myData[3]="txt4";
myData[4]="txt5";
myData[5]="txt6";
$('#display').click(function(){
//Get the user input
for(i=0; i<6; i++)
{
if(document.getElementById(myData[i]).value !=0)
{
var nameInput = document.getElementById(myData[i]).name;
var Quan = document.getElementById(myData[i]).value;
//Create a new row with an ID
var newRow = $('<tr />').attr('id', 'row' + rowNumber);
//Add some HTML to the row
newRow.html('<td>' + nameInput + '</td><td>' + Quan + '</td>');
//Append the new row to the body of the #myTable table
$('#myTable tbody').append(newRow);
//Iterate row number
rowNumber++;
}
}
});
});
I’m inserting the row dynamically. How can I assign the id to each td so I can access it and update it after time. I’m sticking in it.
You don’t really need to. You can use jQuery’s
nth()selector.For example:
Demo here: http://jsfiddle.net/Uz4Ze/
Alternatively, I would suggest assigning each
<td>a class, instead of anid. This will give you the added benefit of being able to style each column differently.