skip to Main Content
<select>
    <option value="America">America</option>
    <option value="North America">North America</option>
</select>

We have an issue where in WP Forms we have a list of countries or regions, and we want to be able to disable any given one as a Parent, such as disabling "America" in the form above. IE. Europe, Great Britain, Spain. We’d like to disable Europe form being selected.

<script type="text/javascript">
(function( $ ) {
  'use strict';
  $(document).ready( function(){
    var $option = $('option:contains("America")');
    $option.attr('disabled',true);
  });
})(jQuery)
</script>

I tried this, as I found it online, but the issue is that for North America, this tool will be disabled as the option "contains" America.

Is there a different "contains" term to use, that means this "specifically and only" in the field….?

2

Answers


  1. You can try attribute Attribute selector.

    Demo:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select>
        <option value="America" selected>America</option>
        <option value="North America">North America</option>
    </select>
    
    
    <script type="text/javascript">
    (function( $ ) {
      'use strict';
      $(document).ready( function(){
        var $option = $('option[value="America"]');
        $option.prop('disabled', true);
        //Unselect selected   
        $("option:selected").removeAttr("selected");
      });
    })(jQuery)
    </script>
    Login or Signup to reply.
  2. For your particular code:

    <script type="text/javascript">
    (function( $ ) {
      'use strict';
      $(document).ready( function(){
        
        $(`option[value="${ CSS.escape('America') }"]`)
          .attr('disabled', true);
        
      });
    })(jQuery)
    </script>

    But to disable several items I would use jQuery::filter():

    <script type="text/javascript">
    (function( $ ) {
      'use strict';
      
      const blacklist = ['America', 'Europe'];
      
      $(document).ready( function(){
      
        $('option')
          .filter((_, option) => blacklist.includes($(option).attr('value')))
          .attr('disabled', true);
          
      });
    })(jQuery)
    </script>      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search