views:

118

answers:

4

Does anyone have any idea what this jQuery selector will do?

object.find('td:eq(1) div div');

I know that the td:eq(1) will get the 2nd td element in the object, but what will the 2 div's referenced at the end of the selector do?

+10  A: 

It finds the second table cell in a row (I think it's 0-based), and then a div inside a div inside that cell.

So like:

<tr>
  <td>
    Not me
  </td>
  <td>
    <div class='parent'>
      <div class='child'>
        This stuff here!
      </div>
    </div>
  </td>
Pointy
+1, it is zero-based.
Andy E
Don't forget that the `div` does not need to be directly under the parent `div` and also that it will match every `div` that has a parent of another `div` and the second table cell. The difference between http://api.jquery.com/descendant-selector/ and http://api.jquery.com/child-selector/ threw me at first.
Mark
Yes indeed, that's true; I hadn't had any coffee yet so there was only so much typing I was willing to do :-)
Pointy
+1  A: 

This first part finds the 2nd <td> overall in the object, not per-row (for that you want :nth-child). The div div part finds divs 2 levels deep inside the selector.

Here's a matching example of object.find('td:eq(1) div div');

<tr>
 <td><td>
 <td>
   <div>
     <span> <!-- Or any wrappers, etc -->
       <div>Found me!</div>
     </span>
   </div>
 </td>
</tr>
Nick Craver
A: 

what will the 2 div's referenced at the end of the selector do?

Find a div inside of a div. (same as the css selector)

kekekela
+1  A: 

As a side-note, you should generally avoid the non-standard jQuery selectors.

When you use only standard CSS selectors, jQuery can pass the selector work off to the fast built-in querySelectorAll function of modern browsers, but when you use :eq it has to go through the relatively slow native-JavaScript Sizzle selector library.

So you might prefer to spell it:

object.find('td').eq(1).find('div div');
bobince