Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 22nd, 2012, 12:17 AM
Registered User
 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default help needed in DOM

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
DOM nodetype tdatu BOOK: Beginning JavaScript 4th Edition 1 March 18th, 2011 07:33 PM
DOM Perseus Java Basics 3 May 4th, 2006 08:50 AM
Parsing xml using DOM balarkavelidi XML 1 January 31st, 2006 10:28 PM
How to .... Dom Document namitjung PHP How-To 4 July 7th, 2005 12:17 AM
DOM PS C++ freezotic BOOK: Beginning Java 2 0 August 3rd, 2004 11:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.