You must create td and text nodes within loop. Your code creates only 2 td, so only 2 are visible. Example:
var table = document.createElement('table');
for (var i = 1; i < 4; i++) {
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
var text1 = document.createTextNode('Text1');
var text2 = document.createTextNode('Text2');
td1.appendChild(text1);
td2.appendChild(text2);
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
}
document.body.appendChild(table);
Answer from Krzysztof on Stack OverflowYou must create td and text nodes within loop. Your code creates only 2 td, so only 2 are visible. Example:
var table = document.createElement('table');
for (var i = 1; i < 4; i++) {
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
var text1 = document.createTextNode('Text1');
var text2 = document.createTextNode('Text2');
td1.appendChild(text1);
td2.appendChild(text2);
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
}
document.body.appendChild(table);
It is because you're only creating two td elements and 2 text nodes.
Creating all nodes in a loop
Recreate the nodes inside your loop:
var tablearea = document.getElementById('tablearea'),
table = document.createElement('table');
for (var i = 1; i < 4; i++) {
var tr = document.createElement('tr');
tr.appendChild( document.createElement('td') );
tr.appendChild( document.createElement('td') );
tr.cells[0].appendChild( document.createTextNode('Text1') )
tr.cells[1].appendChild( document.createTextNode('Text2') );
table.appendChild(tr);
}
tablearea.appendChild(table);
Creating then cloning in a loop
Create them beforehand, and clone them inside the loop:
var tablearea = document.getElementById('tablearea'),
table = document.createElement('table'),
tr = document.createElement('tr');
tr.appendChild( document.createElement('td') );
tr.appendChild( document.createElement('td') );
tr.cells[0].appendChild( document.createTextNode('Text1') )
tr.cells[1].appendChild( document.createTextNode('Text2') );
for (var i = 1; i < 4; i++) {
table.appendChild(tr.cloneNode( true ));
}
tablearea.appendChild(table);
Table factory with text string
Make a table factory:
function populateTable(table, rows, cells, content) {
if (!table) table = document.createElement('table');
for (var i = 0; i < rows; ++i) {
var row = document.createElement('tr');
for (var j = 0; j < cells; ++j) {
row.appendChild(document.createElement('td'));
row.cells[j].appendChild(document.createTextNode(content + (j + 1)));
}
table.appendChild(row);
}
return table;
}
And use it like this:
document.getElementById('tablearea')
.appendChild( populateTable(null, 3, 2, "Text") );
Table factory with text string or callback
The factory could easily be modified to accept a function as well for the fourth argument in order to populate the content of each cell in a more dynamic manner.
function populateTable(table, rows, cells, content) {
var is_func = (typeof content === 'function');
if (!table) table = document.createElement('table');
for (var i = 0; i < rows; ++i) {
var row = document.createElement('tr');
for (var j = 0; j < cells; ++j) {
row.appendChild(document.createElement('td'));
var text = !is_func ? (content + '') : content(table, i, j);
row.cells[j].appendChild(document.createTextNode(text));
}
table.appendChild(row);
}
return table;
}
Used like this:
document.getElementById('tablearea')
.appendChild(populateTable(null, 3, 2, function(t, r, c) {
return ' row: ' + r + ', cell: ' + c;
})
);
Dynamic Tables with Javascript
Build a dynamic table based on Json response.
You could use conditional rendering leveraging Javascript. Assuming a functional component, inside your return:
return(
{ response?.students?.length > 1 && (<div>...your table here... </div>)}
)The table will be shown only if both sides of the && are true, so only if there is more than 1 students.
More on reddit.comAny way to create a dynamic table (that always lists the top liked site by order) ? I want people to be able to vote on the page directly which will affect the position of the site in the table. Preferably HTML5, Javascript, etc.
Sure. Imagine that each row is an object in an array.
[
{
name: "Cleetus' stinky underwear",
description: "A place to buy nasty stinky underwear",
likes: 420,
...
},
...
[Use javascript to sort it by Likes and render it on the page. If you actually want this to be functional you'll need a back-end as well and a database where your websites are stored.
More on reddit.comDynamic load of html table
Yeah it's probably possible, but you haven't provided enough info to give you a more specific answer.
More on reddit.comVideos
I am trying to create a dynamic table that updates based on the number user selected items in the webpage.
I have the following code, where allSelectedPnts dynamically updates:
for ( const pnt of allSelectedPnts ) {let table = document.getElementById(pointData) let row = document.createElement('tr'); let Xpnt = pnt.geometry.attributes.position.array[0] let Ypnt = pnt.geometry.attributes.position.array[1] let Xdata = document.createElement('td') let Ydata = document.createElement('td') Xdata.innerHTML = Xpnt Ydata.innerHTML = Ypnt console.log(Xdata) row.appendChild(Xdata) row.appendChild(Ydata) table.appendChild(row) console.log(table) }
Relevant html:
<table id="pointInfo">
<thead> <th>X</th> <th>Y</th> </thead> <tbody id="pointData">
</tbody> </table> </div>
I get a console log error of :
Cannot read properties of null (reading 'appendChild')at this line: table.appendChild(row)
Any ideas how to fix? I have tried appending to both pointData and pointInfo, I am not sure what I am doing wrong. Console logging both piontData and pointInfo yield null, this is more than likely the problem.
Full code here.