Reputation: 1046
<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
Reputation: 4638
Check the Js fiddle
$(document).ready(function() {
var txt = "Grape";
var element = $( "select option:contains('"+ txt +"')" );
element.remove();
});
Upvotes: 0
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
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
Reputation: 11302
You can use contains selector and remove function.
$('select option:contains(Grape)').remove();
Note: It is better if you give an ID
to your select
element.
Upvotes: 0