Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Children

Posted on 2008-06-19
7
Medium Priority
?
8,806 Views
Last Modified: 2008-07-04
How would I get the first level children and not get the grandchildren as well

for instance

<div id="topLevel">
  <div>
    <div>
    </div>
  </div>
  <div>
  </div>
<div>

so, if I am starting from the div with the ID of "topLevel" how do I get the child div's one level down and not the grandchild div. I've tried getElementByTagName and this returns all three div's when I only want to get 2 of them.
0
Comment
Question by:Hube02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:neeraj523
ID: 21827989
try this
 <%
	Set objXML = Server.CreateObject("Microsoft.XMLDOM")
    sXMLFile = Server.MapPath("\") & "\xmldoc.xml"
    objXML.load(sXMLFile)
	Set oNodes = objXML.selectNodes("//div/*")
    For Each oChild In oNodes
        Response.Write oChild.tagname & "<BR>"
	Next
%>

Open in new window

0
 
LVL 18

Author Comment

by:Hube02
ID: 21828060
You've lost me.

What does VBScript have to do with getting elements in JavaScript.

The purpose of this is so that I can set style properties dynamically.
0
 
LVL 43

Expert Comment

by:David S.
ID: 21828630
Use the childNodes collection, but remember to check if each node is an element node by checking if the node's nodeType property is set to 1 or not.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Author Comment

by:Hube02
ID: 21830015
Not one to sit around and wait, I tried childNodes last night. The problem with this is that if there is some other type of element in there that is returned as well.

For instance if you have:

<div>
  <div></div>
  <span><span>
</div>

Both the div and the span are returned as element type 1. It seems no matter which route I go, I'm getting more information than I want.

Is there any way to tell what type of element each of the childNodes really is? Is it a div, span, ul. Seems to me there should be some way to get only the first level children of a specific element type.
0
 
LVL 43

Assisted Solution

by:David S.
David S. earned 1000 total points
ID: 21830235
> Is there any way to tell what type of element each of the childNodes really is?

Sure. Use the nodeName property. (It's best to use toLowerCase() on it before comparing it to a string.)
0
 
LVL 22

Assisted Solution

by:neeraj523
neeraj523 earned 1000 total points
ID: 21830275
here is a the sample script which will read only divs in the documents..
<div>
	<div>div1</div
	<span>span1</span>
</div>
<SCRIPT>
 
	var divs = document.getElementsByTagName( 'DIV' );
    for ( var i = 0; i < divs.length; i++ ) 
	{
        alert(divs[i].innerText);
    }
</SCRIPT>

Open in new window

0
 
LVL 18

Accepted Solution

by:
Hube02 earned 0 total points
ID: 21880623
I decided to limit nested divs. So each div can only contain one child div. This allows me to use the getElementsByTagName and then take the first one which is the immediate child.

Not the best solution and not quite what I was looking for, but it will work for the intended purpose.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question