Creating table with DOM is invisible with IE ?

G

GRenard

Hi,

I'm trying just to display a table on a webpage using DOM elements
created dynamically.

I really don't understand why IE doesn't display the document
successfully...
If I make a copy/paste of the output, I can see the data.
Mozilla displays successfully a table... Check this little code :

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Test</title>
</head>
<body onload="">
<table>

</table>
<p></p>
</body>

<script type="text/javascript">
var pItem = document.getElementsByTagName("p").item(0);
pItem.appendChild( document.createTextNode("TEST") );

function onLoaded(infoOn) {
var table = document.getElementsByTagName("table").item(0);

for(val in infoOn) {
currentRow = document.createElement("tr");
currentCell = document.createElement("td");
currentText = document.createTextNode(val);
currentCell.appendChild( currentText );
currentRow.appendChild(currentCell);
table.appendChild(currentRow);
}
table.style.visibility = "visible";
table.setAttribute("border", "1");

}
onLoaded(document.getElementsByTagName("table"));
</script>
</html>


On IE, this code "displays" only "TEST" but try Copy/paste and the data
will be pasted... so what is the problem ?

Thank you very much.
 
G

GRenard

Thank you very much, it works perfectly ! It just weird that I can use
tr or td hardcoded but not dynamically directly...

Thank you again !
 
R

RobG

GRenard said:
Hi,

I'm trying just to display a table on a webpage using DOM elements
created dynamically.

I really don't understand why IE doesn't display the document
successfully...
If I make a copy/paste of the output, I can see the data.
Mozilla displays successfully a table... Check this little code :

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Test</title>
</head>
<body onload="">
<table>

</table>
<p></p>
</body>

<script type="text/javascript">
var pItem = document.getElementsByTagName("p").item(0);
pItem.appendChild( document.createTextNode("TEST") );

function onLoaded(infoOn) {
var table = document.getElementsByTagName("table").item(0);

for(val in infoOn) {
currentRow = document.createElement("tr");

As Randy said, IE requires that the tr element be added to a tbody, not
directly to the table (and use var to keep variables local).

Rather than creating a tbody element, you can also use insertRow:

var currentRow = table.insertRow(-1);

Passing a row index of -1 will insert the row as the last row of the table.

currentCell = document.createElement("td");

You can also use:

var currentCell = currentRow.insertCell(-1);

currentText = document.createTextNode(val);
currentCell.appendChild( currentText );
currentRow.appendChild(currentCell);

If insertCell is used, the cell is already added to the row so this line
isn't required.

table.appendChild(currentRow);

If insertRow is used, the row is already added to the table so this line
isn't required either.
 
M

Martin Honnen

GRenard said:
It just weird that I can use
tr or td hardcoded but not dynamically directly...

That is how HTML as an SGML application works, if your document markup
has e.g.
<table>
<tr>
<td>
then the SGML parser adds a tbody element in the DOM tree to be a child
of the table element and the parent of the tr element.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top