Jump to content

appendChild problem with IE7


rollOrDie

Recommended Posts

It doesn't pop up with an error message, it's just that the table just doesn't appear

 

function tableBuild ()
    {
        var container = document.getElementById ('container');
        
        // build head
        var headName = document.createTextNode ('Name');
        var headAge = document.createTextNode ('Age');
        var th1 = document.createElement ('th');
        var th2 = document.createElement ('th');
        var tr = document.createElement ('tr');
        var table = document.createElement ('table');
        
        // append
        th1.appendChild (headName);
        tr.appendChild (th1);
        table.appendChild (tr);
        container.appendChild (table);
        alert (table.nodeName);       
    }
    
window.onload = tableBuild;

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="ajaxStudent.js"></script>
</head>

<div id="container">
This is the container
</div>



<body>
</body>
</html>

 

 

It works okay in FF (surprisingly). Also, when I alert the nodeName, even IE recgnises it as a table, so I don't know why it isn't displaying it?

Link to comment
Share on other sites

try to give your "th" an id; with setAttribute() - example below:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function tableBuild()
    {

var txt = document.createElement("p");
txt.setAttribute("id","testing");
document.getElementById("container").appendChild(txt);
var tester = document.createTextNode("Hello World!");
document.getElementById("testing").appendChild(tester);

    }   

window.onload = function() {
tableBuild();
}

</script>
</head>

<div id="container">
This is the container
</div>



<body>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.