views:

35

answers:

1

I am able to do this:

<div id = "myDiv">
   <div class="foo" ></div>
</div>

myDiv = getElementById("myDiv");
myDiv.querySelectorAll("#myDiv > .foo");

That is, I can successfully retrieve all the direct children of the myDiv element that have class .foo. The problem is, it bothers me that I must include the #myDiv in the selector, because I am running the query on the myDiv element (so it is obviously redundant).
I ought to be able to leave the #myDiv off, but then the selector is not legal syntax since it starts with a >.

Does anyone know how to write a selector which gets just the direct children of the element that the selector is running on?

A: 

You could

myDiv.querySelectorAll('* > .foo');
Anurag
Actually, that does not work. That will retrieve the grandchildren of myDiv as well as the children.
Matt S
@Matt - you are right, * selects all descendants.
Anurag