Creating new table rows with IE

T

Tomasz Cenian

alex bazan napisał(a):
<table id="a" border="1">
<tr>
<td><input type="button" onclick="newItem()" value="new item"></td>
</tr>
</table>

Any suggestions?

Always append new rows to <tbody> instead of <table>, i.e:

<table border="1">
<tbody id="a">
<tr>
<td><input type="button" onclick="newItem()" value="new item"></td>
</tr>
</tbody>
</table>
 
A

alex bazan

This piece of code (which works as a charm with Mozilla) ... does not
append new rows to the table in explorer, although the code is appended
to the innerHTML property.

<html>
<body>

<script>
function newItem () {

newTR=document.createElement('tr');
newTD=document.createElement('td');
newTD.innerHTML="this is a test";
newTD.width="100%";
newTR.appendChild(newTD);
document.getElementById("a").appendChild(newTR);
alert (document.getElementById("a").innerHTML);
}
</script>

<table id="a" border="1">
<tr>
<td><input type="button" onclick="newItem()" value="new item"></td>
</tr>
</table>


</body>
</html>



Any suggestions?

thanks in advance.
alex.
 
A

alex bazan

En/na alex bazan ha escrit:
This piece of code (which works as a charm with Mozilla) ... does not
append new rows to the table in explorer, although the code is appended
to the innerHTML property.

Problem solved. Changed
document.getElementById("a").appendChild(newTR);

TO:

document.getElementById("a").getElementsByTagName('tbody')[0].appendChild(newTR);
 
R

RobG

alex said:
En/na alex bazan ha escrit:
This piece of code (which works as a charm with Mozilla) ... does not
append new rows to the table in explorer, although the code is
appended to the innerHTML property.

Problem solved. Changed
document.getElementById("a").appendChild(newTR);


TO:

document.getElementById("a").getElementsByTagName('tbody')[0].appendChild(newTR);

You could also use the insertRow method of the table:

var x=document.getElementById('a').insertRow(newRowIndex);

where newRowIndex is where to put the new row - 0 is the top,
rows.length will be the last:

var tbl = document.getElementById('a');
var x = tbl.insertRow(tbl.rows.length);

You can then add cells using insertCell(newCellIndex):

var y = x.insertCell(0);
var z = x.insertCell(1);

will insert cells 'y' and 'z' into the new 'x' row.
 
M

Martin Honnen

alex said:
En/na RobG ha escrit:


are this methods standard??

Yes, see the W3C DOM Level 2 HTML recommendation:
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-68927016>
but note that IE has a slightly different implementation (parameter
being optional there):

<http://msdn.microsoft.com/library/d.../author/dhtml/reference/methods/insertrow.asp>

will they work on mozilla?

Yes, at least as posted above if the parameter is present.
 
A

alex bazan

En/na RobG ha escrit:
alex said:
En/na alex bazan ha escrit:
This piece of code (which works as a charm with Mozilla) ... does not
append new rows to the table in explorer, although the code is
appended to the innerHTML property.

Problem solved. Changed
document.getElementById("a").appendChild(newTR);



TO:

document.getElementById("a").getElementsByTagName('tbody')[0].appendChild(newTR);


You could also use the insertRow method of the table:

var x=document.getElementById('a').insertRow(newRowIndex);

where newRowIndex is where to put the new row - 0 is the top,
rows.length will be the last:

var tbl = document.getElementById('a');
var x = tbl.insertRow(tbl.rows.length);

You can then add cells using insertCell(newCellIndex):

var y = x.insertCell(0);
var z = x.insertCell(1);

will insert cells 'y' and 'z' into the new 'x' row.


are this methods standard??
will they work on mozilla?
 
R

RobG

Christopher said:
Why is that? (it's a serious question :))

The tbody element is always present in a table, but the tags are
optional if there is no thead or tfoot.

"The TBODY start tag is always required except when the table
contains only one table body and no table head or foot
sections. The TBODY end tag may always be safely omitted."

<URL:http://www.w3.org/TR/html4/struct/tables.html#edef-TBODY>

Since almost no one codes them in their HTML, browsers insert
the tbody element when parsing the table.

Mozilla et al assume that when you append rows to a table
that you mean to the tbody, IE doesn't. So with IE you must add
rows to the tbody even if it isn't in your HTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top