Access jquery objects with selectors

I have an unordered list with several divs contained inside. Each list item is created using jquery and placed in a variable.

I need a way: nth-child (even) of each div inside a jquery object.

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

How can I use the variable "ele" inside the jquery object to get the nth child contained inside?

What I imagined was not working:

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

Another thing I imagined

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

Also does not work.

+3
source share
1 answer

use .find()

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

crazy demonstration

+2
source

Source: https://habr.com/ru/post/1768928/


All Articles