Connecting Tech Pros Worldwide Forums | Help | Site Map

Return all child nodes from an HTML document

Member
 
Join Date: Jan 2009
Posts: 33
#1: Jan 23 '09
Alright. I'm a php developer, working with JAVA. I've had some luck so far, to the point of being able to remove child nodes in my HTML page. What I need now is a way to return all child node's ids.

Example of my HTML
Expand|Select|Wrap|Line Numbers
  1. <div id="parent">
  2. <span id="child1"></span>
  3. <span id="child2"></span>
  4. </div>
I've tried this:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('divError').innerHTML = '1';
  2. $children = document.getElementById('parent').getChildNodes();
  3. document.getElementById('divError').innerHTML = 'This Works';
without the $children = etc, divError is 'This Works', but when I try to return the child nodes, I only get 1. So, I don't know why, but my javascript is hanging up on that.

Again, what I need is the id of each child node returned.

Any help would be greatly appreciated.

Member
 
Join Date: Jan 2009
Posts: 33
#2: Jan 23 '09

re: Return all child nodes from an HTML document


Well, I think I am getting somewhere...

I was able to get a return value using:

var x=document.getElementById('parent').childNodes;

When I print the variable, I get:

[object Node List]

I'm trying to figure out how to print the actual node list, and so right now I'm not sure whether or not it is returning the information I need.

Thanks again to anyone who can help.
Member
 
Join Date: Jan 2009
Posts: 33
#3: Jan 23 '09

re: Return all child nodes from an HTML document


And again, I find myself trying to access an object, without knowing how.

I was able to get the length of the array, and I thought from there it would be easy. However when I tried to return an element of the array (whose length is 5) using:

document.getElementById('divError').innerHTML = x[x.length-1];

the result was:

[object Text]

So, I can iterate through the array easily enough, but I still need to know how to return the value from an object.
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#4: Jan 23 '09

re: Return all child nodes from an HTML document


document.getElementsByTagName("*") will find all the real tags, which is probably what you want.

there is no default property for every node.

you can use the default operator (||) to fetch something from each type of tag:

a basic "node report" type iteration dump:
Expand|Select|Wrap|Line Numbers
  1.  
  2. var tags = document.body.getElementsByTagName("*"), buff=[];
  3. for(var i=0, mx=tags.length; i<mx;i++){
  4.      var it = tags[i];
  5.      buff[i] =  it.nodeName+" : "+ (it.text || it.value || it.textContent || it.innerText);
  6. }
  7.  
  8. alert(buff.join("\n"));
  9.  
  10.  
Member
 
Join Date: Jan 2009
Posts: 33
#5: Jan 23 '09

re: Return all child nodes from an HTML document


Thank you! I modified it to work with what I need, and I think I understand everything thats going on.

Here is what I used to return the id of the element:

Expand|Select|Wrap|Line Numbers
  1.     var tags = document.body.getElementsByTagName("*"), buff=[];
  2.     for(var i=0, mx=tags.length; i<mx;i++){
  3.          var it = tags[i];
  4.          buff[i] =  it.nodeName+" : "+ (it.id);
  5.     }
  6.  
  7.     alert(buff.join("\n"));
it returned all the id's for the elements. Thank you again!
Reply

Tags
child, html, java, node, return