0

So I want to be able to hide all the options and then show only the ones that don't include hello_ in their value. This includes hello_bye, hello_hello etc. Anything that starts with hello_

This is what I have so far:

jQuery(document).ready(function(){
    jQuery("#metakeyselect > option").hide();
    jQuery("#metakeyselect > option[//what goes here?//]").show();
});

How do I show everything BUT options with values including hello_?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
bigpotato
  • 26,262
  • 56
  • 178
  • 334

3 Answers3

4

You can "hide" the ones whose value start with hello_ using the attribute-starts-with selector.

As Alex pointed out correctly, not all browsers let you hide option elements though (see also How to hide optgroup/option elements?). But you can remove them:

var hidden_options = jQuery("#metakeyselect > option[value^=hello_]").remove();

or disable them:

jQuery("#metakeyselect > option[value^=hello_]").prop('disabled', true);

depending on what else you want to do with them.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

http://api.jquery.com/contains-selector/

Elements that contain hello_:

$("#metakeyselect > option:contains('hello_')")

Elements that do not contain hello_:

$("#metakeyselect > option:not(:contains('hello_'))")
000
  • 26,951
  • 10
  • 71
  • 101
0

You can use:

$(document).ready(function(){
    var options = $('#metakeyselect > option');
    var contains = "hello_";

    options.each(function() {
        if($(this).val().indexOf(contains) != -1){
            $(this).remove();
        }
    });
});

OR

$(document).ready(function(){
    $("#metakeyselect > option").remove();
    $("#metakeyselect > option[value*='hello_']").show();
});

You will need to use remove() for options.

Dani-san
  • 304
  • 2
  • 4