Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an element and its children in jquery? [duplicate]

I have a div with further divs contained in it.These divs internally contain table/div in them. How can I select this parent div and its children in jquery? Parent div has the class "AccordionTitle AccordionTitle-selected"

I have tried following

var klo=$(".AccordionTitle.AccordionTitle-selected").contents()

and

var klo=$(".AccordionTitle.AccordionTitle-selected").children().clone()

but it is only selecting parent div and its children.Its not going deep to the root.I am using jquery 1.9.0

Thanks

like image 563
user2513420 Avatar asked Dec 01 '22 18:12

user2513420


2 Answers

You can use the , separator in order to select multiple queries. Something like this should do.

var klo=$(".AccordionTitle.AccordionTitle-selected, .AccordionTitle.AccordionTitle-selected > *")

This will select the element itself as well as elements right beneath the element itself in the DOM tree.

If you're looking for a solution to find child elements and their child elements too, something like this will do.

var klo=$(".AccordionTitle.AccordionTitle-selected, .AccordionTitle.AccordionTitle-selected *")

The principle is the same, only now you've omitted the > operator which will only allow children right below the original node.

like image 87
Mathias Lykkegaard Lorenzen Avatar answered Dec 10 '22 12:12

Mathias Lykkegaard Lorenzen


Try .addBack()

var klo=$(".AccordionTitle.AccordionTitle-selected").children().addBack()
like image 20
Arun P Johny Avatar answered Dec 10 '22 13:12

Arun P Johny