I have a list item element that contains a label element. I want to select the list item element with the :has()
selector. Inside the label element there is text I want to match with :contains()
. Is it possible to do both of these things with a single line of jQuery? If not, what is an elegant way to select the li element based on the contents of its child label element?
<li>
<!-- I want to select this one -->
<label>Label 1</label>
<!-- more follows -->
</li>
You want either:
$("li:has(label):contains('Label 1')")
This will select any <li>
that both has a <label>
and contains the text Label 1
anywhere inside of it.
$("li:has(label:contains('Label 1'))")
This will select any <li>
that has a <label>
and that label in particular contains the text Label 1
. (The first selector will match if "Label 1
" is anywhere inside the <li>
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With