How to select elements from a set that are children of another element

Posted by Tatu Ulmanen on Stack Overflow See other posts from Stack Overflow or by Tatu Ulmanen
Published on 2010-06-16T09:00:08Z Indexed on 2010/06/16 9:02 UTC
Read the original article Hit count: 243

Filed under:

Hi,

The title is a bit confusing but here's what I'm after:

  • I have a set of elements containing all the h3 elements in a page.
    In jQuery terms: var mySet = $('h3').

  • I also have an div element var myContainer = $('div#foo')

  • I need to find all items in the set mySet that are children of myContainer.

Any ideas? I bet there's some magical one liner for this but I cannot think of any. I'd rather not loop through every element in the set manually and use .closest(myContainer) or something like that to determine the relationship.

Note that in my scenario, I cannot use a new selector like $('div#foo h3') (that would be too easy) as I don't have access to the actual selector values. So it has to be dynamic.

© Stack Overflow or respective owner

Related posts about jQuery