SoFunction
Updated on 2025-04-03

Javascript traversal elements in table example code

For example:

<table id=tb>
 <tr><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th></tr>
 <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
 <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

And how to traverse assignments in JS?
Program code

var count=0;
for(i=0; i < ("tb1").; i++)
{
for(j=0; j < ("tb1").rows(i).; j++)
{
("tb1").rows(i).cells(j).innerText = count;
count++;
}
}

I saw another way to use dom (but I didn't try it myself, I don't know why)

&lt;table &gt;
&lt;tr&gt;&lt;td&gt;A monk has water to drink。&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Two monks carry water to drink。&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Three monks have no water to drink。&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;script&gt;
var msg=””
var mylist=("mylist")
for (i=0; i&lt;; i++){
var tr=[i];
for(j=0;j&lt;[j].length; j++) {
var td=[j];
msg+=;
}
}
alert(msg);
&lt;/script&gt;