I am trying to read a table (table id= "Table1") in a document and find all instances of tag names "span" extract the value contained in each span tag.
I used the following and it does not work:
function flag() {
var text = ""
var table1 = document.getElementById("Table1");
var test1 = table1.getElementsByTagName("span");
var children = test1.childNodes;
for(i = 0; i < children.length; i ++) {
text += children.nodeValue;
}
alert(text);
}
The error I am getting is 'children has no properties'.
Your help is appreciated.
In case you would like to see the table:
<table id="Table1" border="1" cellspacing="1" cellpadding="2" width="100%">
<tr color="yellow">
<td width="120" style=" background-color: rgb(255,255,153);" height=" 23">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Last name</span></b>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">First name</span></b>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Date</span></b>
</p>
</td>
<td width="73" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Acct Balance</span></b>
</p>
</td>
<td width="89" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Monthly transactions</span></b>
</p>
</td>
</tr>
<tr>
<td width="120" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Smith</span>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Peter</span>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Oct 5, 2005</span>
</p>
</td>
<td align="right" width="73" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">1,560.25</span>
</p>
</td>
<td align="right" width="89" style="background-color: rgb(255,255,255);">
<p style="text-align: right; padding-right: 5pt;">
<span style="font-size: x-small;">20</span>
</p>
</td>
</tr>
<tr>
<td width="120" style="background-color: rgb(255,255,255);"height=" 18">
<p>
<span style="font-size: x-small;">Serenty</span>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">John</span>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Oct 7, 2005</span>
</p>
</td>
<td align="right" width="73" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">261.00</span>
</p>
</td>
<td align="right" width="89" style="background-color: rgb(255,255,255);">
<p style="text-align: right; padding-right: 5pt;">
<span style="font-size: x-small;">3</span>
</p>
</td>
</tr>
</table>
I used the following and it does not work:
function flag() {
var text = ""
var table1 = document.getElementById("Table1");
var test1 = table1.getElementsByTagName("span");
var children = test1.childNodes;
for(i = 0; i < children.length; i ++) {
text += children.nodeValue;
}
alert(text);
}
The error I am getting is 'children has no properties'.
Your help is appreciated.
In case you would like to see the table:
<table id="Table1" border="1" cellspacing="1" cellpadding="2" width="100%">
<tr color="yellow">
<td width="120" style=" background-color: rgb(255,255,153);" height=" 23">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Last name</span></b>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">First name</span></b>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Date</span></b>
</p>
</td>
<td width="73" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Acct Balance</span></b>
</p>
</td>
<td width="89" style="background-color: rgb(255,255,153);">
<p style="background-color: rgb(255,255,153);">
<b><span style="font-size: xx-small; color: rgb(102,102,51);">Monthly transactions</span></b>
</p>
</td>
</tr>
<tr>
<td width="120" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Smith</span>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Peter</span>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Oct 5, 2005</span>
</p>
</td>
<td align="right" width="73" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">1,560.25</span>
</p>
</td>
<td align="right" width="89" style="background-color: rgb(255,255,255);">
<p style="text-align: right; padding-right: 5pt;">
<span style="font-size: x-small;">20</span>
</p>
</td>
</tr>
<tr>
<td width="120" style="background-color: rgb(255,255,255);"height=" 18">
<p>
<span style="font-size: x-small;">Serenty</span>
</p>
</td>
<td width="88" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">John</span>
</p>
</td>
<td width="100" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">Oct 7, 2005</span>
</p>
</td>
<td align="right" width="73" style="background-color: rgb(255,255,255);">
<p>
<span style="font-size: x-small;">261.00</span>
</p>
</td>
<td align="right" width="89" style="background-color: rgb(255,255,255);">
<p style="text-align: right; padding-right: 5pt;">
<span style="font-size: x-small;">3</span>
</p>
</td>
</tr>
</table>