I have the following structure
<ul>
<li><a ...>something</a></li>
...
<li><a ...>blabla</a></li>
<ul>
I need to remove the li
element where the text of the anchor is blabla.
How would i go about selecting this element?
$(--what selector here--)
or do I need to loop over each li
and compare its .text()
value to 'blabla'?
If you want a contains (substring match) then :contains()
works:
$('li:contains("blabla")').remove();
If you want an exact match, e.g. not matching "blablabla", you can use .filter()
:
$('li').filter(function() { return $.text([this]) === 'blabla'; }).remove();
$('li > a:contains("blabla")').remove();
Have a look at the :contains selector.
I've just noticed that :contains
does partial matching. You may need to do...
$('li > a:contains("blabla")').each(function() {
if ($(this).text() === 'blabla') {
$(this).parent().remove();
}
});
You could also make the selector less strict if doing it that way.
... or you could do it much neater like Nick Craver.
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