How to select an item and its children in jquery?

advertisements

This question already has an answer here:

  • jQuery select descendants, including the parent 8 answers

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


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.