4

If I have following jQuery selector specified :

$("div#myDivId .myClass")

What is the exact selection order? Will this first select a div with an ID myDivId and then find all elements with myClass inside it or first select all elements with myClass and then filter out rest which are not inside myDivId.

Niranjan Borawake
  • 1,628
  • 13
  • 20
  • Why don't you [take a peek at the unminified source](http://code.jquery.com/jquery-latest.js) and check it out? – Marty Sep 09 '13 at 07:12
  • 2
    The general question in your title depends on whether jQuery is able to pass the string to _querySelectorAll()_ or not, which will depend on whether you've used jQuery custom selectors (and on whether the browser even supports _querySelectorAll(),_ etc.). – nnnnnn Sep 09 '13 at 07:16
  • @MartyWallace Tried doing that but got lost. If I don't get an answer will do that again. Thanks. – Niranjan Borawake Sep 09 '13 at 08:08
  • @nnnnnn : For now I am not interested in custom selectors, I just care about available selectors. Thanks. – Niranjan Borawake Sep 09 '13 at 08:14
  • I don't mean custom selectors in the sense of ones that _you_ define, I mean selectors that are "available" by default in jQuery but not part of the CSS specification, like the [`:input` selector](http://api.jquery.com/input-selector/). – nnnnnn Sep 09 '13 at 10:58
  • [This](http://learn.jquery.com/performance/optimize-selectors/) helped. – Niranjan Borawake Dec 19 '13 at 05:05

1 Answers1

5

Sizzle API (that used by jQuery) uses "right-to-left" order of selector's tokens "execution".

For the modern browsers that support QSA (native querySelectorAll function) Sizzle will actually delegate the work to it that also "right-to-left".

Here is performance comparison: Sizzle vs. QSA, also I've create sample for your case: http://jsperf.com/does-order-of-selectros-matters

Another question is why they are "right-to-left"? See here: https://stackoverflow.com/questions/5797014/why-do-browsers-match-css-selectors-from-right-to-left

Community
  • 1
  • 1
Eugene D. Gubenkov
  • 5,127
  • 6
  • 39
  • 71