Kor
05-20-2005, 05:11 AM
I wanna get all the text nodes of the children of an element. The goal is to get an array with all the textNodes in each cell of a table, without any other nodes that might ocure whithin that cell (<p>, <br> etc...)
I mean if:
<td>12</td>
<td>1<p>23<b>34</b>5</p>6</td>
I need:
var txt = new Array()
txt[0] = '12';
txt[1] = '123456';
Now I had to circle through all the childNodes to extract all the text nodes. I have build a function, but something is wrong in the code, and I don't sense what. I need soime fresh eyes, any ideeas? Where's the mistake?:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function checkCell(){
var allC = document.getElementById('tab').getElementsByTagName('td');//cells' collection
var txt = new Array()
for(var i=0;i<allC.length;i++){
txt[i]='';
var chC = allC[i].childNodes;
for(var j=0;j<chC.length;j++){
while(chC[j].hasChildNodes()){
chC[j]=chC[j].firstChild;
}
txt[i]+=chC[j].data;
}
}
alert(txt[1])
}
onload=checkCell;
</script>
</style>
</head>
<body>
<table id="tab" width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>12</td>
<td>1<p>23<b>34</b>5</p>6</td>
</tr>
</table>
</body>
</html>
It looks like
while(obj.hasChildNodes()){
obj = obj.childNodes[0]
}
or
while(obj.hasChildNodes()){
obj = obj.firstChild;
}
is not correct. Why?
I mean if:
<td>12</td>
<td>1<p>23<b>34</b>5</p>6</td>
I need:
var txt = new Array()
txt[0] = '12';
txt[1] = '123456';
Now I had to circle through all the childNodes to extract all the text nodes. I have build a function, but something is wrong in the code, and I don't sense what. I need soime fresh eyes, any ideeas? Where's the mistake?:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function checkCell(){
var allC = document.getElementById('tab').getElementsByTagName('td');//cells' collection
var txt = new Array()
for(var i=0;i<allC.length;i++){
txt[i]='';
var chC = allC[i].childNodes;
for(var j=0;j<chC.length;j++){
while(chC[j].hasChildNodes()){
chC[j]=chC[j].firstChild;
}
txt[i]+=chC[j].data;
}
}
alert(txt[1])
}
onload=checkCell;
</script>
</style>
</head>
<body>
<table id="tab" width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>12</td>
<td>1<p>23<b>34</b>5</p>6</td>
</tr>
</table>
</body>
</html>
It looks like
while(obj.hasChildNodes()){
obj = obj.childNodes[0]
}
or
while(obj.hasChildNodes()){
obj = obj.firstChild;
}
is not correct. Why?