Alex Rivera | Logout

jquery remove list item where .text() = 'blabla'

Asked 2010-09-14T23:29:30.253
14

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'?

Edit
Report

1 Answer

1

If you want to avoid removing lis containing blabla unless blabla is further nested, use this:

$("ul li > a:contains('blabla')").parent().remove();

Otherwise, the first li from this HTML will be removed even though "blabla" is not where you might be targeting it:

<ul>
    <li>
        <div>
            Some nested content here:
            <span>
                Blablabla
            </span>
        </div>
    </li>
    <li>Some text</li>
</ul>
answered 2010-09-14T23:34:30.017

Your Answer