1

For example,

var relevantItems = ul.querySelectorAll('li.someClass[data-x=someData]')

Should only match these elements from the outer, referenced <ul>:

<ul>
  <!-- This <li> matches -->
  <li class="someClass" data-x="someData">
    <ul>
      <!-- depending on the query, there may be some extra matches within the hierarchy, but these should not be searched or included in the results -->
    </ul>
  </li>
  <!-- need to also includes additional matches like this one: -->
  <li class="someClass" data-x="someData">
  </li>
</ul>

Right now I have a couple of options:

  1. Use JavaScript to loop over childNodes/children array and compare the className and dataset.x variables traditionally.

  2. Give each <ul> its own id and use the #id> prefix feature like so:

    ul.querySelectorAll('#' + ul.id + ' > li....')
    

Is there a simpler syntax, or alternative function, e.g. filtering the childNodes List somehow, to achieve the same query? (without adding an external library i.e. jQuery)

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • 1
    There doesn't appear to be a "good" way. http://stackoverflow.com/questions/3680876/using-queryselectorall-to-retrieve-direct-children – Paul S Dec 04 '16 at 19:12
  • Thanks! That thread has the answer I was looking for: http://stackoverflow.com/a/21126966/463304 – 700 Software Dec 04 '16 at 19:38

0 Answers0