Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a selected item from a dropdown list (Using Jquery)

Tags:

jquery

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)?

like image 928
Biki Avatar asked Aug 05 '10 09:08

Biki


2 Answers

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.

like image 92
Jacob Relkin Avatar answered Sep 21 '22 22:09

Jacob Relkin


something like this:

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

or even shorter:

$('#cmbTaxID :selected').remove();
like image 30
VeeWee Avatar answered Sep 19 '22 22:09

VeeWee