0

I have tried everything i know to remove some of these option values

here is the html - I want to remove every option that has a value with MESSAGE in it

<select id="source_list" name="HOME_MODULES_AVAILABLE" size="16">
 <option value="TWITTER_FEED=N"></option>
 <option value="MFLTIP=N"></option>
 <option value="WHO_SHOULD_I_START=N"></option>
 <option value="LEAGUE_MAP=W"></option>
 <option value="MESSAGE=W"></option>
 <option value="MESSAGE2=W"></option>
 <option value="MESSAGE3=W"></option>
 <option value="MESSAGE4=W"></option>

I have tried all of these and no luck

$('#source_list:contains("MESSAGE")').hide();
$("option:contains('MESSAGE')").hide();
$("#source_list option[value="MESSAGE"]").hide();
$("select > option[value*='MESSAGE']").remove();
$("select > option[value$='MESSAGE']").remove();
$("#source_list > option[value*='MESSAGE']").remove();

any advice ?

MShack
  • 642
  • 1
  • 14
  • 33

4 Answers4

0

Try this:

$('#source_list option').each(function(i, e) {
    if($(e).attr('value').match(/^MESSAGE/)) $(e).remove();
});

It iterates through every option node and checks with a regular expression, whether value starts with "MESSAGE".

It is by the way better to remove the options instead of trying to hide them, due to better cross browser support.

Lionel
  • 1,949
  • 3
  • 17
  • 27
  • "When should I vote down? Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." What of this is the reason for your downvote(s)? – Lionel Aug 15 '14 at 07:59
0

There are two ways to do this

1) starts with selector ^ - it checks for all options value starting with the word MESSAGE.

$("#source_list > option[value^='MESSAGE']").remove();

DEMO

2) contains selector * - it checks for all options value containing the word MESSAGE.

$("#source_list > option[value*='MESSAGE']").remove();

DEMO

Pbk1303
  • 3,702
  • 2
  • 32
  • 47
-1

Try using CSS:

option[value="MESSAGE"] {
    display:none;
}

If you want it to be toggle-able add the following class to the option tags you want to be able to hide and show.

.hidden {
    display:none;
}

then use .addClass and .removeClass via jQuery.

-2
$("#selectboxid option[value='1']").remove();
frogatto
  • 28,539
  • 11
  • 83
  • 129
CSK
  • 777
  • 7
  • 17