F
F
Finom2011-12-27 03:08:41
css
Finom, 2011-12-27 03:08:41

querySelectorAll function and first child selector?

Let's say I have some element node. It is necessary, without resorting to cycles, childNodes and matchesSelector , to find first-level descendants that match some selector, for example 'div.classname1.classname2[title$="bla"]'. One solution is the following:

node.querySelectorAll('#node1 > div.classname1.classname2[title$="bla"]'); //при условии, что node.id==="node1"
Obviously, this only helps if the selector node (in this case "#node1") is known and unique.
I came up with this crutch:
var id = node.dataset.randomId = Math.random();
node.querySelectorAll('[data-random-id="'+id+'"] > div.classname1.classname2[title$="bla"]');

But adding unnecessary attributes to elements to the right and left is not a good solution.
Recording
node.querySelectorAll('> div.classname1.classname2[title$="bla"]');

gives a syntax error
Sobsno, how to get the children of an element that matches the selector using querySelectorAll?
Maybe there is some selector that selects the current element, something like Andrei ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2011-12-27
@k12th

http://ejohn.org/blog/thoughts-on-queryselectorall/

This is a spec bug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question