How to select all <h/> descendants of a context element, except <h/> descendants of <section/> descendants of the context
I want to select all <h/> descendants of a context element, except <h/> descendants of <section/> descendants of the context
In other words, given HTML,
<whatever>
<section> <!-- This element is the context -->
<whatever>
<h>Match</h>
<section>
<h>Don't match</h>
</section>
</whatever>
</section>
</whatever>
- and the identified context element, I want to select <h>Match</h>, but not <h>Don't match</h>
Which of the following two ways is better?
$('h', context).not($('section h', context))
- or -
$('h', context).filter(function ()
{
return !jQuery.contains(context, $(this).closest('section')[0]);
})
Is there a still better way?
The following doesn't work - it excludes both <h>Match</h> and <h>Don't match</h>,
$('h:not(section h)', context)