Aravinth
Aravinth

Reputation: 139

How to give ID for TableRow using insertRow()?

 var table = document.getElementById("table1");
    var tr = table.insertRow();
    var td = tr.insertCell();
    td.innerHTML= document.getElementById('txt1').value;

i am using code like this. how to give Id for tr(TableRow). help me.

Upvotes: 9

Views: 46592

Answers (4)

Reporter
Reporter

Reputation: 3948

because you use standard Javascript you have to use the function setAttribute("align", "center", 0);.

Try following:

tr.setAttribute("id", "myIdNameforThisRow", 0);

Upvotes: 7

aswathy
aswathy

Reputation: 395

create a variable first like var id=0;

then

tr.id=id;
id++;

use this code

Upvotes: 0

AmGates
AmGates

Reputation: 2123

Just give tr.id = "some_id". This will work.

Upvotes: 18

DaveRandom
DaveRandom

Reputation: 88697

var table = document.getElementById("table1");
var tr = table.insertRow();
tr.id = 'id_for_this_row'; // Just assign a value to the new row's 'id' attribute
var td = tr.insertCell();
td.innerHTML= document.getElementById('txt1').value; 

Upvotes: 2

Related Questions