Brian K Brian K - 2 months ago 11
HTML Question

Uisng jQuery to target all dl tags who do not have a specific parent

I'm trying to write a jQuery function that select and receives the text from a website that is designed like this:

<dl>
<dd> hey </dd>
</dl>
<dl>
<dd> how's it goin</dd>
</dl>
<div style="border:1px solid grey; background:#eee; padding:1em;">
<dl>
<dd>don't select this</dd>
</dl>
</div>


When I use this code:

$("dd").each(function(){
transcript += $(this).text();
})


I select the text within all 3 of the dl tags, however, I only want to select the text within the first 2 dl tags. Is there any way to filter out the text I get by somehow saying that I don't want the text from dl tags that have a parent div with the specific style said in the code I posted? Thanks for the help!

Answer

You can try this:

$("dd").not("div dl dd");