Utku Utku - 3 months ago 10
HTML Question

Select elements whose specific descendants' HTML contains a specific text?

I want to select all

div
which has a
cite
descendant and that
cite
descendant's HTML contains some
text
. I have tried:

$('div')
.filter('cite')
.filter(function() {
return $(this).html().match('text');
})


and this:

$('div')
.filter('div cite')
.filter(function() {
return $(this).html().match('text');
})


but it is not working. What is missing here?

That is, I want to select the 3rd
div
in the following HTML.

HTML:

<div id="abcd">text1 text2</div>
<div id="abccd">text1 text2</div>
<div id="abcccd">
<cite><b>text1</b>text2</cite>
</div>
<div id="abd">text1 text2</div>


EDIT:
The thing I want to select is not
$('div cite')
. I want to select
$('div')
, where
div
s have a
cite
descendant and that
cite
descendant's contents contain some text.

Answer

Use :contains() selector:

$("div").find("cite:contains('text')").closest("div");
Comments