1

If, on css we have:

#divitis>*

So I believe, it (the browser) will start reading all elements and then, check the #divitis element, so, it will go from right to left.

cf. Why do browsers match CSS selectors from right to left?

My question is:

Does anyone knows if the children() jquery function works similarly, when we do:

$('#divitis').children()

Will it work from right to left:

  1. Check all elements child of the child of the child;
  2. Check all elements that are child of the child;
  3. Check all elements that are child;
  4. Grab the first children of #divitis;

OR, from left to right:

  1. Scan all elements and search from #divitis;
  2. Scan the first children of divitis;

Thanks in advance.

Community
  • 1
  • 1
MEM
  • 30,529
  • 42
  • 121
  • 191
  • It's not overly clear what you're asking I'm afraid. – BenM Jul 29 '13 at 09:35
  • 1
    `children()` isn't a selector. Which bit do you mean that you want to read right to left? – Jivings Jul 29 '13 at 09:35
  • 3
    I don't know about right to left(since the DOM is layed out top to bottom) but some quick tests would suggest that it does it top-bottom (the natural flow of the DOM). – reggaemahn Jul 29 '13 at 09:38
  • Maybe this will help you http://stackoverflow.com/questions/9843142/change-in-order-for-each-in-firefox-and-chrome – Maxim Jul 29 '13 at 09:46
  • possible duplicate of [Is the order objects are return by a jQuery selector specified?](http://stackoverflow.com/questions/1636201/is-the-order-objects-are-return-by-a-jquery-selector-specified) – Álvaro González Jul 29 '13 at 09:47
  • @ÁlvaroG.Vicario I'm not asking if there is of there isn't an order. I'm asking if that order, even if only eventually, is of certain kind, similar to the selectors on CSS. I guess I'm not asking the same. – MEM Jul 29 '13 at 09:50
  • @MEM - Ah, you are asking whether jQuery follows the W3C CSS specs regarding selectors. Well, it should, and it it doesn't then it's a bug. – Álvaro González Jul 29 '13 at 09:55
  • @ÁlvaroG.Vicario No, he's just asking in which order the individual terms within the selector string are parsed. – Alnitak Jul 29 '13 at 11:26

2 Answers2

0

.children() will only look at the direct children of your selector. What you need is .find() instead, which in my experience will traverse the DOM tree "in order", so from "left to right", meaning direct children first, then children of children, and so on.

Added a little JSFiddle for clarification: http://jsfiddle.net/qbG7x/

UweB
  • 4,080
  • 2
  • 16
  • 28
0

In your case where you've written $('#divitis').children(), you've actually got two different selectors, i.e. #divitis and > *.

However, because the two jQuery function calls are followed strictly in normal evaluation order, the #divitis selector will be evaluated first, and then > *.

This is therefore the opposite order to if this had been written just as a single selector containing #divitis > *.

That said, the reason why the browsers go R->L instead of L->R doesn't really apply here.

When applying a CSS style sheet it's more efficient to skip mismatching individual rules of a CSS style sheet based on the rightmost element of each selector than on the leftmost element.

Here, though, you're programmatically "writing a single rule", so there's no "other rules" that need skipping.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I still don't get it, sorry. You said that the reason why browsers go R->L instead of L->R doesn't apply. But you also say that having `$('#divitis > *')` is the opposite order then `$('#divitis').children()` ... at the end, I didn't get if it works differently of not. Sorry, I'm not that proficient in English. At a first glass, I would say that `#divitis>*` may be slower then `$('#divitis').children()` – MEM Jul 31 '13 at 09:55
  • @MEM that would be my assessment - `#divitis > *` is slower than `#divitis .children()`. The R->L order is there to allow the browser to trivially discard CSS rules that don't match. In a line of jQuery code there are no "non-matching" rules, there's just one "rule". – Alnitak Jul 31 '13 at 15:58