brian wilson
brian wilson

Reputation: 495

remove li containing string

I want to trigger a delete of all LI within this UL if any of the LI contain stockTo.

This below, is not working.

var stockTo = 'abc';

$('ul#thelst li:contains(stock)').remove();

Upvotes: 1

Views: 3044

Answers (5)

thecodeparadox
thecodeparadox

Reputation: 87073

$('ul#thelst li).contains('+ stockTo +').remove();

Upvotes: 2

Murtaza
Murtaza

Reputation: 3065

hey the way you have written your jQuery is not proper..

check the live demo here. How to remove list

BELOW IS THE HTML

<div>
    <ul id="thelst">
        <li>test test test test 111 </li>
        <li>test test test test 112 </li>
        <li>test test test test 113 </li>
        <li>test test test test 114 </li>
        <li>test test test test 115 </li>
        <li>test test test test 116 </li>
    </ul>
</div>

THIS IS THE JQUERY CODE INSIDE DOCUMENT READY FUNCTION

var stockTo = '114';

$('#thelst').find("li:contains(" + stockTo  + ")").remove();

EDIT DEMO 2 removing the list on click of anchor

Upvotes: 1

G&#225;bor Nagy
G&#225;bor Nagy

Reputation: 189

$('#thelst li:contains(' + stockTo + ')').remove();​

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34107

Hiya demo http://jsfiddle.net/m7zUh/

It will be good to see how your HTML look like but see the code below.

In the sample example: Click on trigger button and you will see foo will get removed as li

Hope this helps, :) have a good one, cheers

HTML

<div> sample</div>
<ul id="thelst"> 
    <li>Hulk</li>
        <li>Hawk</li>
        <li>Foo</li>
        <li>Bar</li>
        <li>Rambo</li>
    </ul>

<input type="button" id="trigger" value="trigger" />
​

Jquery code

$('#trigger').click(function() {

    var stockTo = 'Foo';
    $('ul#thelst li:contains(' + stockTo + ')').remove();

});​

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

Try this:

var stockTo = 'abc';
$('ul#thelst li:contains(' + stockTo + ')').parent().children().remove();

Upvotes: 3

Related Questions