Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using querySelectorAll, is there a way to reference the immediate children of the context node, without using IDs?

Say I have an HTML structure like

<div id="a">
  <div id="b">
    <div id="c"></div>
  </div>
</div>

To do a query for the children of "a" using querySelectorAll I can do something like

//Get "b", but not "c"
document.querySelectorAll('#a > div')

My question is: is it possible to do this without the ID, referencing the node directly? I tried doing

var a_div = document.getElementById('a')
a_div.querySelectorAll('> div') //<-- error here

but I get an error telling me that the selector I used is invalid.


And in case anyone is wondering, my real use case would be something more complicated like '> .foo .bar .baz' so I would prefer to avoid manual DOM traversal. Currently I am adding a temporary id to the root div but that seems like an ugly hack...

like image 924
hugomg Avatar asked Jul 11 '12 20:07

hugomg


People also ask

What does the querySelectorAll () method do?

Document.querySelectorAll() The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.

Can you use querySelectorAll with ID?

Use the querySelectorAll() method to select elements by multiple ids, e.g. document. querySelectorAll('#box1, #box2, #box3') . The method takes a string containing one or more selectors as a parameter and returns a collection of the matching elements. Here is the HTML for the examples in this article.

How do you get children of querySelectorAll?

Use the :scope pseudo-class to get the direct children of an element using querySelectorAll, e.g. parent. querySelectorAll(':scope > div') . When used with the querySelectorAll method, :scope matches the element on which the method was called.

How does the querySelector () method differ from querySelectorAll ()?

Differences: As seen above, querySelector() methodcan only be used to access a single element while querySelectorAll() method can be used to access all elements which match with a specified CSS selector. To return all matches, querySelectorAll has to be used, while to return a single match, querySelector is used.


2 Answers

document.querySelector('#a').querySelectorAll(':scope > div')

I am not sure about browser support, but I use it on chrome and chrome mobile packaged apps and it works fine.

like image 173
Peter StJ Avatar answered Sep 20 '22 07:09

Peter StJ


No, there isn't a way (yet) to reference all childs of some element without using a reference to that element. Because > is a child combinator, which represents a relationship between a parent and child element, a simple selector (a parent) is necessary (which is missing in you example).

In a comment, BoltClock said that the Selectors API Level 2 specification defines a method findAllname may change"which accepts as an argument what will probably be known as a relative selector (a selector that can start with a combinator rather than a compound selector)".
When this is implemented, it can be used as follows:

a_div.findAll('> div');
like image 40
Rob W Avatar answered Sep 19 '22 07:09

Rob W