How can I hide an option of a select by it's text value?

Dinshaw Raje

I have the below code in my dropdown list:

<select id="state_code" name="account[state_code]">
    <option value="">Select State</option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="AE">Armed Forces</option>
    <option value="AA">Armed Forces</option>
    <option value="AP">Armed Forces</option>
    <option value="AS">translation missing: en.world.us.as.name</option>
    <option value="DC">translation missing: en.world.us.dc.name</option>
    <option value="GU">translation missing: en.world.us.gu.name</option>
    <option value="MP">translation missing: en.world.us.mp.name</option>
    <option value="PR">translation missing: en.world.us.pr.name</option>
    <option value="UM">translation missing: en.world.us.um.name</option>
    <option value="VI">translation missing: en.world.us.vi.name</option>
</select>

I want hide all options whose text starts with translation missing: en.world.us. I have tried the below, but its not working.

var ab = "translation missing: en.world"
$('#state_code').find('option[text^="' + ab + '"]').hide();

Please help me out. Thanks in advance.

Rory McCrossan

The text of an element is not an attribute, so the attribute selector will not work in thise case, as you've discovered. Instead you can use a combination of a regular expression and the filter() function to achieve this:

$('#state_code option').filter(function() {
    return /^translation missing:/i.test($(this).text());
}).hide();

Example fiddle

I want to hide now both translation missing: en.world.us and Armed Forces

For this you can use the 'or' character in regex: |. What you tried won't work as the logic flow is flawed due to two return statements. Try this:

$('#state_code option').filter(function() {
    return /^translation missing:|armed forces/i.test($(this).text());
}).hide();

Example fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How I can hide div on option select

From Dev

how i can select then hide <a> link based on its text

From Dev

How to get the value of a select's option by its text

From Dev

How I get the text instead value from a select option in CodeIgniter

From Dev

How to hide label attribute of select's option?

From Dev

How can I set a select option value without using jQuery?

From Dev

How can I get value when click option in the select on vue?

From Dev

How can I get selectize to insert the text of the option, not just the value?

From Dev

Hide select option value so user can not view it

From Dev

How can I hide default <select> <option> when the drop-down is clicked?

From Dev

How can I create a javascript to show and hide info according option value selected?

From Dev

How can I hide my drop-down menu option(s)?

From Dev

How can I pre-select an option in a select menu based on a value stored in the database?

From Dev

How can I customize a select dropdown option

From Dev

How can I select and option with jquery?

From Dev

How do I get the text and not the value of an Option?

From Dev

In Javascript how do I add a new option into Html Select so inserted based on alphabetical value of text

From Dev

How to update select text with matched option value text

From Dev

How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input

From Dev

How can i get text option from AJAX generated select dropdown using jQuery?

From Dev

Form select option, can't get the value and display text right

From Dev

How can I get tag's text value with xsl transformation

From Dev

How can I permanently disable the auto-hide taskbar option?

From Dev

Find select option value by text

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

jquery click select option value with option text

From Dev

how can i create the label when ever i select the spinner value i.e Official and when ever i select spinner value normal hide the label?

From Dev

Can I add custom attributes to an option value in select(drop down)

Related Related

  1. 1

    How I can hide div on option select

  2. 2

    how i can select then hide <a> link based on its text

  3. 3

    How to get the value of a select's option by its text

  4. 4

    How I get the text instead value from a select option in CodeIgniter

  5. 5

    How to hide label attribute of select's option?

  6. 6

    How can I set a select option value without using jQuery?

  7. 7

    How can I get value when click option in the select on vue?

  8. 8

    How can I get selectize to insert the text of the option, not just the value?

  9. 9

    Hide select option value so user can not view it

  10. 10

    How can I hide default <select> <option> when the drop-down is clicked?

  11. 11

    How can I create a javascript to show and hide info according option value selected?

  12. 12

    How can I hide my drop-down menu option(s)?

  13. 13

    How can I pre-select an option in a select menu based on a value stored in the database?

  14. 14

    How can I customize a select dropdown option

  15. 15

    How can I select and option with jquery?

  16. 16

    How do I get the text and not the value of an Option?

  17. 17

    In Javascript how do I add a new option into Html Select so inserted based on alphabetical value of text

  18. 18

    How to update select text with matched option value text

  19. 19

    How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input

  20. 20

    How can i get text option from AJAX generated select dropdown using jQuery?

  21. 21

    Form select option, can't get the value and display text right

  22. 22

    How can I get tag's text value with xsl transformation

  23. 23

    How can I permanently disable the auto-hide taskbar option?

  24. 24

    Find select option value by text

  25. 25

    How can I disabled select option(s) base on the JSON data got from AJAX?

  26. 26

    How can I disabled select option(s) base on the JSON data got from AJAX?

  27. 27

    jquery click select option value with option text

  28. 28

    how can i create the label when ever i select the spinner value i.e Official and when ever i select spinner value normal hide the label?

  29. 29

    Can I add custom attributes to an option value in select(drop down)

HotTag

Archive