vicky
vicky

Reputation: 1046

Removing options fom Select using Jquery

<select>
    <option value="Value 1">Apple</option>
    <option value="Value 2">Mango</option>
    <option value="Value 3">Grape</option>
    <option value="Value 4">Banana</option>
</select>

And I have String "Grape" and the result output should be like this.

<select>
    <option value="Value 1">Apple</option>
    <option value="Value 2">Mango</option>
    <option value="Value 4">Banana</option>
</select>

How to do that?

Upvotes: 0

Views: 1650

Answers (4)

Arun Bertil
Arun Bertil

Reputation: 4638

Check the Js fiddle

http://jsfiddle.net/CZL2y/

$(document).ready(function() {
 var txt = "Grape";
var element = $( "select option:contains('"+ txt +"')" );
element.remove();
});

Upvotes: 0

bipen
bipen

Reputation: 36531

give your select an id(unique) , use id selector to get the select element. and use :contains selector

$(function(){ 
  $('#givenSelectID option:contains("Grape")').remove();
});

Upvotes: 0

Lix
Lix

Reputation: 47966

You'll need to use the jQuery :contains() selector -

:contains() - Select all elements that contain the specified text.

For example:

var txt = "Grape";
var element = $( "#selectElement option:contains('"+ txt +"')" );
element.remove(); // now just remove it

Upvotes: 1

letiagoalves
letiagoalves

Reputation: 11302

You can use contains selector and remove function.

$('select option:contains(Grape)').remove();

Working demo

Note: It is better if you give an ID to your select element.

Upvotes: 0

Related Questions