JQuery Remove selected option from list

I have the following selection field (I cannot add a class or identifier to the selection form, so I wrapped it in a div):

<div id="test"><select name="wpv-yr">
<option value="0" selected="selected">All Years</option>
<option value="2010">2010 (6)</option>
<option value="2011">2011 (12)</option>
<option value="2012">2012 (32)</option>
<option value="2013">2013 (129)</option>
<option value="2014">2014 (24)</option>
</select></div>

And I do not want to show the latest version of 2014 in the drop-down list. I tried to add:

jQuery(document).ready(function() {
jQuery("div#test option[value=2014]").remove();
});

I also tried single quotes around the value:

jQuery(document).ready(function() {
jQuery("div#test option[value='2014']").remove();
});

But that didn't work either. Option 2014 is still listed.

Where am I mistaken?

+4
source share
3 answers

I just created fiddlehere and it works. Are you linking to the library JQueryon the page? The only change in my code replaces JQuerywith$

$(document).ready(function () {
    $("div#test option[value=2014]").remove();
});
+15
source

Try

jQuery('div#test option[value="2014"]').remove();

or

jQuery("div#test option:last-child").remove();
+3

JQuery

$("#List option:selected").remove();

fiddle

+1

All Articles