Hi, i am writing a simple parser using JavaScript that parses a simple HTML. In one part of the code i retireive all children of a node, in my body (html) i have only three elements <h1><div><input>, however my code output some #text tags along with them, i tried outputting the content and it says undefined??? really puzzled here, i have removed all white spaces still the same...
CODE:------------------------------------------------
HTML Code:
<html>
<head>
<script>
function outPutNodeName(child1)
{
ndName = child1.nodeName.toLowerCase();
if(ndName == "#text")
{
document.write("node name = " + ndName + " and text = " + ndName.nodeValue + "</br>");
}
else
{
document.write("node name = " + ndName + "</br>");
}
}
function countChildren(node1) // counts children of the passed tag
{
var chren = node1.childNodes; // Now get all children of n
for(var i=0; i < chren.length; i++)
{
outPutNodeName(chren[i]);
}
document.write("No of children = " + i + "</br>");
}
function testTags(node)
{
var child = node.firstChild; // the root of is the document then come HTML tag
var type = child.nodeType;
countChildren(document.body);
}
</script>
</head>
<body>
<h1 id ="foo">hello</h1>
<div>hello</div>
<input type="button" value="Add Child Element" onclick="testTags(document)">
</body>
</html>
:------------------------------------------------
OutPut:
node name = #text and text = undefined
<----------what is this element
node name = h1
node name = #text and text = undefined
node name = div
node name = #text and text = undefined
node name = input
node name = #text and text = undefined
No of children = 7