Approach: Select the option from select which needs to remove. Use JQuery remove() method to remove the option from the HTML document.
To remove all options from a select list using jQuery, the simplest way is to use the empty() method.
Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').
This post will discuss how to remove the selected option from the dropdown list with jQuery. With jQuery, you can use the . remove() method to takes elements out of the DOM. To get the selected item from a dropdown, you can use the :selected property and call remove() on the matched element.
Try this:
$(".ct option[value='X']").each(function() {
$(this).remove();
});
Or to be more terse, this will work just as well:
$(".ct option[value='X']").remove();
$('.ct option').each(function() {
if ( $(this).val() == 'X' ) {
$(this).remove();
}
});
Or just
$('.ct option[value="X"]').remove();
Main point is that find
takes a selector string, by feeding it x
you are looking for elements named x
.
find()
takes a selector, not a value. This means you need to use it in the same way you would use the regular jQuery function ($('selector')
).
Therefore you need to do something like this:
$(this).find('[value="X"]').remove();
See the jQuery find docs.
It works on either option tag or text field:
$("#idname option[value='option1']").remove();
If no id or class were available for the option values, one can remove all the values from dropdown as below
$(this).find('select').find('option[value]').remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With