5

Shouldn't the following two approaches to selecting a node produce the same result?

let tmp = fruits.querySelector("ul:first-of-type li:first-of-type");
tmp = tmp.querySelector("span")    

vs.

let tmp = fruits.querySelector("ul:first-of-type li:first-of-type span");

(See it in action here)

I've tested this in both firefox and chrome. Different results in both cases. Can anybody please explain why?

Example in stack snippet:

let fruits = document.querySelector("[data-segment='fruits']");
console.log(fruits);
let tmp = fruits.querySelector("ul:first-of-type li:first-of-type")
tmp = tmp.querySelector("span")
console.log("Works:")
console.log(tmp)
console.log("Does not work:")
console.log(fruits.querySelector("ul:first-of-type li:first-of-type span"))
<main id="app" data-v-app="">
  <section>
    <h2>Tree</h2>
    <ul role="tree">
      <li role="treeitem" data-segment="fruits" aria-level="1" aria-setsize="3" aria-posinset="1" aria-expanded="true"><span tabindex="0">Fruits</span>
        <ul role="group">
          <li role="none" data-segment="oranges" aria-level="2" aria-setsize="5" aria-posinset="1"><span tabindex="-1">Oranges</span>
            <!--v-if-->
          </li>
          <li role="none" data-segment="pineapple" aria-level="2" aria-setsize="5" aria-posinset="2"><span tabindex="-1">Pineapple</span>
            <!--v-if-->
          </li>
          <li role="treeitem" data-segment="apples" aria-level="2" aria-setsize="5" aria-posinset="3" aria-expanded="false"><span tabindex="-1">Apples</span>
            <ul role="group">
              <li role="none" data-segment="macintosh" aria-level="3" aria-setsize="3" aria-posinset="1"><span tabindex="-1">Macintosh</span>
                <!--v-if-->
              </li>
              <li role="none" data-segment="granny_smith" aria-level="3" aria-setsize="3" aria-posinset="2"><span tabindex="-1">Granny Smith</span>
                <!--v-if-->
              </li>
              <li role="none" data-segment="fuji" aria-level="3" aria-setsize="3" aria-posinset="3"><span tabindex="-1">Fuji</span>
                <!--v-if-->
              </li>
            </ul>
          </li>
          <li role="none" data-segment="bananas" aria-level="2" aria-setsize="5" aria-posinset="4"><span tabindex="-1">Bananas</span>
            <!--v-if-->
          </li>
          <li role="none" data-segment="pears" aria-level="2" aria-setsize="5" aria-posinset="5"><span tabindex="-1">Pears</span>
            <!--v-if-->
          </li>
        </ul>
      </li>
      <li role="none" data-segment="vegetables" aria-level="1" aria-setsize="3" aria-posinset="2"><span tabindex="-1">Vegetables</span>
        <!--v-if-->
      </li>
      <li role="none" data-segment="grains" aria-level="1" aria-setsize="3" aria-posinset="3"><span tabindex="-1">Grains</span>
        <!--v-if-->
      </li>
    </ul>
  </section>
</main>
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
Michael Lipp
  • 341
  • 3
  • 8
  • I ran it and same result in 3 different browsers. – Masood Apr 11 '21 at 10:45
  • 1
    [The last example pretty much explains why it works that way](https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector) – Jay Nyxed Apr 11 '21 at 10:55
  • the most upsetting thing for me is that `fruits.querySelector("ul:first-of-type li:first-of-type")` selects the *Oranges* `
  • ` while `fruits.querySelector("ul:first-of-type li:first-of-type span")` selects the *Fruits* ``
  • – asdru Apr 11 '21 at 11:14
  • 2
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+queryselector+matches+element+outside+of+parent) of [Why does querySelector('div span') match even though querySelector('div') does not?](https://stackoverflow.com/q/64522987/4642212) and [queryselectorAll with descendant not selecting correctly](https://stackoverflow.com/q/49545252/4642212). – Sebastian Simon Apr 11 '21 at 13:20