/    Sign up×
Community /Pin to ProfileBookmark

Dynamic Table creation not working

Hi,

The following code for dynamic table creation is not working.
Can anyone please help me.
The dynamically created rows and columns are not getting populated.

CODE:
=========

<html>
<head>
<title>Sample code – Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function startLoad() {
var tbl = document.getElementById(“tbl”);
var tblBody = document.getElementById(“tblBody”);
var newTblBody = document.createElement(“newTblBody”);
newTblBody.id = “tblBody”;

for (var j = 0; j < 2; j++) {
// creates a table row
var row = document.createElement(“tr”);

for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement(“td”);
var cellText = document.createTextNode(“cell is row ” + j + “, column ” + i);
cell.appendChild(cellText);
row.appendChild(cell);
}

// add the row to the end of the table body
newTblBody.appendChild(row);
}

// put the <tbody> in the <table>
tbl.replaceChild(newTblBody, tblBody);
// sets the border attribute of tbl to 2;
tbl.setAttribute(“border”, “2”);
}
</script>
</head>

<body onload=”startLoad()”>
<table id=”tbl” border=”1″ width=”100%” cellspacing=”0″>
<thead>
<tr>
<td class=”header”>Col 1</td>
<td class=”header”>Col 2</td>
</tr>
</thead>

<tbody id=”tblBody”>
</tbody>
</table>
</body>

</html>

===========================


_________________
  • Arunan
  • to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @KDLASep 26.2007 — I haven't tested the js, but your coding for the table is incorrect. It should be:

    <table id="tbl" border="1" width="100%" cellspacing="0">

    <thead>

    <tr>

    [B]<th class="header">Col 1</th>

    <th class="header">Col 2</th>[/B]


    </tr>

    </thead>

    <tbody id="tblBody">

    </tbody>

    </table>
    Copy linkTweet thisAlerts:
    @arunankauthorSep 26.2007 — Hi KDLA,

    Thanks for your reply.

    Just now I came to know that there is a big mistake in this code.

    While creating table body element,

    var newTblBody = document.createElement("newTblBody");

    should be,

    var newTblBody = document.createElement("tbody");

    Now the code works fine. Thanks much.

    Thanks and regards,

    Arunan
    ×

    Success!

    Help @arunank spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...