270

I have a page with 5 selects that all have a class name 'ct'. I need to remove the option with a value of 'X' from each select while running an onclick event. My code is:

$(".ct").each(function() {
    $(this).find('X').remove();
   });

Where am I going wrong?

Kara
  • 6,115
  • 16
  • 50
  • 57
user135498
  • 6,013
  • 8
  • 29
  • 29

13 Answers13

535

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();
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 2
    This worked for me. Thanks. What's the sytax to limit this to .ct with a selected value = '' ? – user135498 Oct 05 '09 at 04:12
  • 2
    This is the easiest if you don't need to know the value. For example removing the first option $("#affiliate")[0][0].remove(); – ladieu Mar 06 '14 at 20:13
66
$('.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.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 6
    +1 i like this answer i think its better because i can test 'X' as like or part of the `value` for ` – shareef Aug 15 '13 at 09:10
  • 1
    I used this answer as i had to remove all the options other than val = 0. i used != 0 and worked like charm :) – Lakshman Pilaka Nov 19 '15 at 06:51
38

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
TM.
  • 108,298
  • 33
  • 122
  • 127
9

It works on either option tag or text field:

$("#idname option[value='option1']").remove();
Ashu
  • 479
  • 6
  • 6
6

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();
Vinod VT
  • 6,946
  • 11
  • 51
  • 75
Deepa
  • 61
  • 1
  • 1
3

Iterating a list and removing multiple items using a find. Response contains an array of integers. $('#OneSelectList') is a select list.

$.ajax({
    url: "Controller/Action",
    type: "GET",
    success: function (response) {
        // Take out excluded years.
        $.each(response, function (j, responseYear) {
            $('#OneSelectList').find('[value="' + responseYear + '"]').remove();
        });
    },
    error: function (response) {
        console.log("Error");
    }
});
CYoung
  • 307
  • 1
  • 10
2

Something that has quickly become my favorite thing to do with removing an option is not to remove it at all. This method is beneficial for those who want to remove the option but might want to re-add it later, and make sure that it's added back in the correct order.

First, I actually disable that option.

$("#mySelect").change(
    function() {

        $("#mySelect").children('option[value="' + $(this).val() + '"]').prop("disabled", true);

        $("#re-addOption").click(
            function() {
                $("#mySelect").children('option[value="' + howeverYouStoredTheValueHere + '"]').prop("disabled", false);
            }
        );
    }
);

and then to clean up, in my CSS, I set disabled options to be hidden, because hiding an option in some browsers doesn't work, but using the method above, clients with those browsers wont be able to select the option again.

select option[disabled] {
    display: none;
}

Personally, on the re-addOption element, I have a custom property of data-target="value", and in place of howeverYouStoredTheValueHere, I use $(this).attr('data-target').

Ethan Moore
  • 383
  • 1
  • 5
  • 18
2

To remove all Options

$(".select").empty();

To remove all Options and add a blank option

$(".select").empty().append(new Option('--Select--',''));
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
1

if your dropdown is in a table and you do not have id for it then you can use the following jquery:

var select_object = purchasing_table.rows[row_index].cells[cell_index].childNodes[1];
$(select_object).find('option[value='+site_name+']').remove();
Md. Shafiqur Rahman
  • 2,878
  • 27
  • 24
1

For jquery < 1.8 you can use :

$('#selectedId option').slice(index1,index2).remove()

to remove a especific range of the select options.

jajhonrod
  • 69
  • 3
1

When I did just a remove the option remained in the ddl on the view, but was gone in the html (if u inspect the page)

$("#ddlSelectList option[value='2']").remove(); //removes the option with value = 2
$('#ddlSelectList').val('').trigger('chosen:updated'); //refreshes the drop down list
Vishav Premlall
  • 456
  • 6
  • 22
1

Try this for remove the selected

$('#btn-remove').click(function () {
    $('.ct option:selected').each(function () {
        $(this).remove();
    });
});
1

I tried this code:

$("#select-list").empty()

Walk
  • 1,531
  • 17
  • 21