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