Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select ALL children (in any level) from a parent in jQuery?

I have to .unbind() all elements from a parent node.

How can I select all children (at any level) from a parent?

Tried :

$('#google_translate_element *').unbind('click'); 

but it works only for the first children's level...

Here there is a test case

like image 537
markzzz Avatar asked Oct 04 '11 13:10

markzzz


People also ask

How do you get children of children in jQuery?

Definition and Usage. 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.

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

Which 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 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.


2 Answers

Use jQuery.find() to find children more than one level deep.

The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.

$('#google_translate_element').find('*').unbind('click'); 

You need the '*' in find():

Unlike in the rest of the tree traversal methods, the selector expression is required in a call to .find(). If we need to retrieve all of the descendant elements, we can pass in the universal selector '*' to accomplish this.

like image 173
Konerak Avatar answered Sep 22 '22 02:09

Konerak


I think you could do:

$('#google_translate_element').find('*').each(function(){     $(this).unbind('click'); }); 

but it would cause a lot of overhead

like image 20
Nicola Peluchetti Avatar answered Sep 21 '22 02:09

Nicola Peluchetti