Vignesh Shiv
Vignesh Shiv

Reputation: 1157

Delete Dropdown values using jquery

I have a situation here,

Using JQuery I'm appending some values in drop down list.. Even it is one or two value that appended in drop down list..

For Add,

tableui+='<option value="">'+resourceadd+'</option>';
$('#resourcess').append(tableui); 

When the page reloads automatically the value stored in db.

For example 4 values added in dropdown list (Values are not stored in db), I want to delete last value. I'm using,

$("#resourcess :last-child").remove();

The same condition, I want to delete middle of two values, How to do it??

Upvotes: 1

Views: 78

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Assuming you know the value of the option you want to remove, you could use filter:

$('#resources option').filter(function() {
    return this.value == 'foo'; // insert your value here.
}).remove();

Or an attribute selector:

$('#resources option[value="foo"]').remove();

If you don't know the value, but do know the position of the option within the select, you could remove it by index using eq():

$('#resources option').eq(1).remove(); // remove the 2nd option

Upvotes: 2

Related Questions