Radley Sustaire
Radley Sustaire

Reputation: 3399

Accessing jquery objects with selectors

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.

Upvotes: 3

Views: 177

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

use .find()

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

crazy demo

Upvotes: 2

Related Questions