Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove options from select

People also ask

How to remove select options using jQuery?

Approach: Select the option from select which needs to remove. Use JQuery remove() method to remove the option from the HTML document.

How remove all option from select tag in jQuery?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method.

How do you remove all options from select in jQuery except first?

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)').

How do I remove a selected dropdown?

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();