Pittfall
Pittfall

Reputation: 2851

How do you remove <li> from a <ul> by Id?

I have:

<ul id="ulId">
    <li id="firstli">
    </li>
    <li id="secondli">
    </li>
</ul>

and I am trying to remove secondli.

This is what I have tried based on some reading.

$('#ulId secondli').remove();

and

$('#ulId > secondli').remove();

but these 2 methods did not work. Any suggestions?

Upvotes: 24

Views: 100782

Answers (5)

pingle60
pingle60

Reputation: 776

You need to add a space between the select the li element,

$('#second li').remove(); // when referencing by id

or

$(".second li").remove(); // when referencing by class

Upvotes: 1

nOcTIlIoN
nOcTIlIoN

Reputation: 1

if you want remove an <li> with ID try :

$('#yourlist #yourli').remove();

otherwise, if you would remove an <li> based on the content you can use :

$('#yourlist li:contains("TextToRemove")').remove();

Upvotes: 0

Zach Shallbetter
Zach Shallbetter

Reputation: 1911

$('#ulId #secondli').remove();

Select the container #ulId and then the list item #secondli. You had forgotten to add the ID tag before the second li.

Upvotes: 0

Praveen Dabral
Praveen Dabral

Reputation: 2509

This will work for you

$('#secondli').remove();

Upvotes: 7

j08691
j08691

Reputation: 207861

Try this:

$('#secondli').remove();

In jQuery, you refer to the ID of an element with a # in front of it. See http://api.jquery.com/id-selector/

Upvotes: 58

Related Questions