tags:

views:

44

answers:

4

Hi, How to remove one or more selected items in option tag, from a HTML dropdown list (Using Jquery).

For removing entire options from a combo box we can use the below Jquery statement.

$("#cmbTaxIds >option").remove();

Assuming the below HTML code in aspx file.

            <select id="cmbTaxID" name="cmbTaxID" style="width: 136px; display: none" tabindex="10" disabled="disabled">
                <option value="0"></option>
                <option value="3"></option>
                <option value="1"></option>
            </select>

If I want to remove only the middle value, then what should be the syntax for the same (using Jquery)?

+3  A: 

Use the eq selector.

var index = $('#cmbTaxID').get(0).selectedIndex;
$('#cmbTaxID option:eq(' + index + ')').remove();

This is the best way to do it because it's index-based, not arbitrary value-based.

Jacob Relkin
It helped to solve my problem. Thanks a lot :-)
Biki
@Biki, can you mark this as the accepted answer?
Jacob Relkin
A: 
$("#cmbTaxIds >option[value='3']").remove();

Just replace 3 with the value of the element you want to remove.

Senseful
A: 

A more generic answer to remove the selected option could be

$('#somebutton').click(function(){
    var optionval = $('#cmbTaxIds').val();
    $('#cmbTaxIds > option[value=' + optionval + ']').remove();

})
Kristoffer S Hansen
A: 

something like this:

$('#cmbTaxID option:selected').remove();

or even shorter:

$('#cmbTaxID :selected').remove();

VeeWee