I am trying to create a simple javascript html table so I can put dynamic data in to it but it wont show when I check my browser. Could someone please tell me what I am doing wrong and how I can get it to show on the page the way I have it? Thank you!
<head>
<title>Test JS Table</title>
<script Language="JavaScript">
var nrCols=2;
var nrRows=4;
var root=document.getElementById('mydiv');
var tab=document.createElement('table');
tab.className="mytable";
var tbo=document.createElement('tbody');
var row, cell;
</script>
<style type="text/css">
.mytable {
border-collapse:collapse;
width:200px;
}
.mytable td{
border:1px solid #000000;
}
</style>
</head>
<body>
<script language="javascript">
tab.className="mytable";
for(var i=0;i<nrRows;i++){
row=document.createElement('tr');
for(var j=0;j<nrCols;j++){
cell=document.createElement('td');
cell.appendChild(document.createTextNode("ROW DATA HERE..."))
row.appendChild(cell);
}
tbo.appendChild(row);
}
tab.appendChild(tbo);
root.appendChild(tab);
</script>
<div id="mydiv"></div>
</body>
</html>
You have to put the script in to
window.onload. When you execute the script the div does not show yet, so it cannot get id for youOr put the script below the div