Skip to content Skip to sidebar Skip to footer

Getting The 2nd Child Element

I am still new in jquery and I have this code
abcsdf
first child
second child
I wanted to get the

Solution 1:

A number of ways to do this one. I'm going to assume the toplevel div has an id of 'top'. This is probably the best one:

$('#top > :nth-child(2)').whatever();

or

$('#top').children(':first-child').next().whatever();

or if you know for a fact there are at least 2 children

$($('#top').children()[1]).whatever();

Solution 2:

check this link

Nth child selecter

Or you can try :eq Selector also

Eq selector

Solution 3:

Use the nth-Child Selector. For example: $('div:nth-child(2)')

Solution 4:

Maybe it sounds silly but $(".item").first().next() does the trick.

Solution 5:

What about giving the div an id and then just grabbing it by $('#mySecondDiv'), depends how you dynamically generate it though...

Post a Comment for "Getting The 2nd Child Element"