Link to home
Start Free TrialLog in
Avatar of Hube02
Hube02Flag for United States of America

asked on

Get Children

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.
Avatar of neeraj523
neeraj523
Flag of India image

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

Avatar of Hube02

ASKER

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.
Avatar of David S.
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.
Avatar of Hube02

ASKER

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.
SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial