Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding row to midle of table dynamically. Is it possible?

Status
Not open for further replies.

Maim

Programmer
Jun 25, 1999
106
CA
Hi, I'm looking for information on whether it's possible to add a row in the middle of a table. Ex. my Table contains 2 <tr>s, add a <tr> between the original two. The cide below just appends to the table but I haven't found how to insert after the indicated row...

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Untitled</title>
</head>
<script language=&quot;javascript&quot;>
function addRow(thisObj)
{
	var theTable = thisObj.parentNode.parentNode;
	var theTBody = thisObj.parentNode;
	var theCell_1;
	var theCell_2;
	var theCell_3;
	var rowAdd = document.createElement('tr');
//	alert('table contents = ' + theTable.innerHTML + '\n\nbody contents = ' + theTBody.innerHTML + '\n\nrow contents = ' + thisObj.innerHTML);
	theCell_1 = document.createElement('td');
	theCell_1.innerHTML = 'empty';
	theCell_2 = document.createElement('td');
	theCell_2.innerHTML = 'test';
	theCell_3 = document.createElement('td');
	theCell_3.innerHTML = 'again';
	rowAdd.id = thisObj.id + '_1';
	theTBody.appendChild(rowAdd);
	rowAdd.appendChild(theCell_1);
	rowAdd.appendChild(theCell_2);
	rowAdd.appendChild(theCell_3);
}
</script>
<body>
<table cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;1&quot;>
<tr id=&quot;r1&quot;>
<td><input type=&quot;checkbox&quot;></td><td>qwert</td><td>werty</td>
</tr>
<tr id=&quot;r2&quot;>
<td><input type=&quot;checkbox&quot;></td><td>asdfg</td><td>sdfgh</td>
</tr>
</table>
<button onclick=&quot;addRow(document.all.r1)&quot;>add</button>
</body>
</html>
[\code]

Any help is appreciated

-----------------------------------
&quot;Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.&quot; - Rich Cook
 
How about this?

function addRow(thisObj){
thisObj.insertAdjacentHTML(&quot;afterEnd&quot;,&quot;<tr><td></td><td>test1</td><td>test2</td></tr>&quot;);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top