Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Direct Child of $(this) in Jquery

Tags:

I know that I can use $('img', this) to select all img elements in $(this).

In my current case I am trying to manipulate only DIRECT child images of $(this).

Is there a selector for that??

like image 934
SideDishStudio Avatar asked Dec 14 '10 20:12

SideDishStudio


People also ask

How do you get the children of the $( this selector?

Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.

What will select all direct child elements in jQuery?

The ("parent > child") selector selects all elements that are a direct child of the specified element.

How do you get children of children in jQuery?

The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.


2 Answers

$(this).find('> img') 

Read this

like image 189
Gabi Purcaru Avatar answered Oct 21 '22 08:10

Gabi Purcaru


$(this).children('img'); 

See also: http://api.jquery.com/children/

like image 30
Michal Avatar answered Oct 21 '22 10:10

Michal