Question

I have a select that contain this values:

<select id="lstCities" class="valid" name="City">
<option value="OSNY">OSNY</option>
<option value="dd">dd</option>
<option value="OSffNY">OSffNY</option>
<option value="ANTONY">ANTONY</option>
<option value="0">Autre...</option>
</select>

How can I delete all options but i would like to keep only

 <option value="0">Autre...</option>

My problem is my list is dynamic sometimes I have 3,5,7,.... select + the last one <option value="0">Autre...</option>

Was it helpful?

Solution

select all options, then exclude the one with that value :

$('#lstCities option[value!="0"]').remove();

FIDDLE

OTHER TIPS

Try

$('#lstCities option:lt(-1)').remove()

Demo: Fiddle

$('#lstCities option[value!=0]').remove()

You should remove by value and not rely on the position of the option element to keep.

If the option is always the last one, I hope this JavaScript code can help you:

var lastNode = $("#lstCities option").last();
var option = { value:lastNode.val(), text:lastNode.text() };

$('#lstCities').find('option').remove().end().append($('<option>',{
        value:option.value,
        text:option.text,
}));

Learned from this post

This worked for me:

$("#selectList option").remove();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top