tags:

views:

20

answers:

1

I have an unordered list with several divs contained inside. Each list item is created with jquery and put into a variable.

I need a way to select the :nth-child(even) of each div within the jquery object.

ele = myList.append("<li> \
<div class='name'>"+this.name+"</div> \
<div class='test'>test</div> \
</li>");

How would I use the variable "ele" within a jquery object to get the nth-child contained within?

What I had imagined, which doesn't work:

$(ele+" div:nth-child(even)").addClass("my-class");

Another thing I imagined

ele.children("div:nth-child(even)").addClass('my-class'));

Also doesn't seem to work.

+1  A: 

use .find()

ele.find("div:nth-child(even)").addClass('my-class');

crazy demo


Welcome to stackoverflow.com
Don't forget to accept an answer

Reigel