Naruto
Naruto

Reputation: 9634

Simple way to reset the dropdown list in jquery

i want to clear only 3 elements in dropdown list. how to do by iteration i.e indexbased would be fine.

Here is how my dropdown looks.

<div id = "Dropdown">
    <select id="dropdownlist" class="trace dropdown" name="dropdownlistitems">                  
        <option value="ID1" selected="selected">--Please Select--</option> 
        <option value="ID2">ID2text</option>
        <option value="ID3">ID3text</option>
    </select>
</div>

Upvotes: 0

Views: 2842

Answers (2)

mridula
mridula

Reputation: 3283

Do you mean something like this?

$('#dropdownlist option').each(function (index, option) {
    if(index!=0)
    {
        $(this).remove();
    }
});

This will remove each option tag that the loop iterates through (except, of course, the first one)

Upvotes: 1

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

How about this?

$('#dropdownlist option:lt(3)').remove();

This selects the first 3 options and removes them.

Upvotes: 0

Related Questions