tags:

views:

68

answers:

4

If I have:

<div id='parent'> 
  <table> 
        <tr> 
            <td> 
                <div id='child1'></div> 
            </td> 
        </tr> 
        <tr> 
             <td> 
                <div id='child2'></div> 
             </td> 
        </tr> 
    </table>
 </div> 

I tried: $('#parent> table > tr:eq(1) > div');

I would like to select a certain child div at its index. For example, I would like to select the second child div child2. A trivial solution is:

var div2 = $('#child2');

But I would rather like to do so with the parent div:

var div2 = $('#parent div')...get(1); // 1 is the index.

Would this be possible?

+2  A: 

You can do it using the :eq() selector like this:

$("#parent > div:eq(1)")

Or if it's dynamic and you need to pass it in, use .eq() like this:

$("#parent > div").eq(1)

In both of these we're using 1 because it's a 0-based index, so 1 is the 2nd child.

Nick Craver
Is the return value the OP's dom element or a jquery object?
Rabid
@Rabid - It's a jQuery object, e.g. you could call `.addClass("active")` at the end. I don't follow on this part though "the OP's dom element"...not sure what you mean there? The OP never mentioned wanting it one way or another (note that in his example is *is* a jQuery object though), *usually* you'd want the jQuery wrapper version.
Nick Craver
Ah yes referring to his example that is the case, good point.
Rabid
Hi Nick Carver,Thanks for the answer. It should work as I stated above but when I check it again, my structure is different, it is:<div id='parent'><table><tr><td> <div id='child1'></div></td></tr><tr><td> <div id='child2'></div></td></tr><tr><td> <div id='child3'></div></td></tr></div>I tried: $('#parent> table > tr:eq(1) > div'); but it is undefined?Thanks in advance.
JoesyXHN
@JoesyXHN - A `<tbody>` gets inserted in there, so the `table > tr` won't work, try using just a space :)
Nick Craver
I am sorry, what did you mean with using just a space? :=)Thanks!
JoesyXHN
@JoesyXHN - Remove the `>` between `table` and `tr` in your selector, so it reads `table tr` instead of `table > tr`, make sense?
Nick Craver
@Nick Craver: yes, it is working ;) I altered it a bit. I made a sample here: http://jsfiddle.net/VLhdw/ Could you please explain me when and why we need to use '>' ? Thanks in advance.
JoesyXHN
@JoesyXHN - You don't usually *need* to, only if you want *only* direct children (e.g. if you may have child tables you want to ignore for example). For your example you can leave all the `>` out :)
Nick Craver
@Nick Caver: Thanks ;)
JoesyXHN
A: 

You will want the children() function

E.g:

$('#parent').children()[1];

Rabid
I was wrong with my structure, any ideas how to deal with it now? Thanks in advance.
JoesyXHN
A: 

Use the :nth-child() selector.

E.g.

$('#parent div:nth-child(2)');

Update due to comment

$('#parent > div:nth-child(2)');
Fermin
Keep in mind that if the children have children, this may select them as well (if they have more than 1 `<div>` as siblings).
Nick Craver
Updated answer to fix this.
Fermin
I was wrong with my structure, any ideas how to deal with it now? Thanks in advance.
JoesyXHN
A: 

There are couple of simple ways to do this:

var div2 = $("#parent > div:eq(1)")

When using the "eq" selector, note that it is zero-based, so this will, in fact, select the 2nd div within the #parent div.

A second way is:

var div2 = $("#parent > div:nth-child(2)")

The "nth-child" selector is not zero based, so "nth-child(2)" will also select the 2nd div. You can also select odd and even divs via "nth-child(even)" and "nth-child(odd)". It will even evaluate equations in the parenthesis - you can read more about it in the jQuery documentation here.

jbyrd
I was wrong with my structure, any ideas how to deal with it now? Thanks in advance.
JoesyXHN