jquery-uijquery-ui-selectmenu

jQueryUI selectmenu - Set selected option


How can I set the selected option on jQueryUI selectmenues?

I try to set the value of the second dropdown in the screenshot below, to "Prof" before disabling it.

enter image description here

I tried it like this:

$salutation.find('option').filter(function() {
    return ($(this).text().includes('Prof'));
}).prop('selected', true);

and like this:

$salutation.find('option').each(function() {
    var optionText = $(this).text();
    if (optionText.includes('Prof'))
        $(this).prop('selected', true);
});

None worked.

Full Example:

$( function() {

  $( "#speed" ).selectmenu();
  $( "#salutation" ).selectmenu();
    
  $speed = $('#speed');
  $salutation = $('#salutation');
  test();
  

  $speed.on('selectmenuchange', function() {
	    test();
  });

  
  function test()
  {
      let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();

      if ( ! selectedOption.includes('fast')) {
        
        $salutation.find('option').filter(function() {
            return ($(this).text().includes('Prof'));
        }).prop('selected', true);
        
        $salutation.selectmenu('disable');
        $salutation.selectmenu('refresh');
        
      } else {
        $salutation.selectmenu('enable');
      }
  }
  
});
fieldset {
  border: 0;
}
label {
  display: block;
  margin: 30px 0 0 0;
}
.overflow {
  height: 200px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


 
<div class="demo">
 
<form action="#">
 
  <fieldset>
    <label for="speed">Select a speed</label>
    <select name="speed" id="speed">
      <option>Slower</option>
      <option>Slow</option>
      <option selected="selected">Medium</option>
      <option>Fast</option>
      <option>Faster</option>
    </select>
  
    <label for="salutation">Select a title</label>
    <select name="salutation" id="salutation">
      <option disabled selected>Please pick one</option>
      <option>Mr.</option>
      <option>Mrs.</option>
      <option>Dr.</option>
      <option>Prof.</option>
      <option>Other</option>
    </select>
  </fieldset>
</form>
</div>


Solution

  • I found the solution. I had to call $salutation.selectmenu('refresh'); after $salutation.selectmenu('disable');

    http://api.jqueryui.com/selectmenu/#method-refresh

    $( function() {
    
      $( "#speed" ).selectmenu();
      $( "#salutation" ).selectmenu();
        
      $speed = $('#speed');
      $salutation = $('#salutation');
      test();
      
    
      $speed.on('selectmenuchange', function() {
    	    test();
      });
    
      
      function test()
      {
          let selectedOption = $speed.find('option:selected').text().trim().toLowerCase();
    
          if ( ! selectedOption.includes('fast')) {
            
            $salutation.find('option').filter(function() {
                return ($(this).text().includes('Prof'));
            }).prop('selected', true);
            
            $salutation.selectmenu('disable');
            $salutation.selectmenu('refresh');
            
          } else {
            $salutation.selectmenu('enable');
          }
      }
      
    });
    fieldset {
      border: 0;
    }
    label {
      display: block;
      margin: 30px 0 0 0;
    }
    .overflow {
      height: 200px;
    }
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    
     
    <div class="demo">
     
    <form action="#">
     
      <fieldset>
        <label for="speed">Select a speed</label>
        <select name="speed" id="speed">
          <option>Slower</option>
          <option>Slow</option>
          <option selected="selected">Medium</option>
          <option>Fast</option>
          <option>Faster</option>
        </select>
      
        <label for="salutation">Select a title</label>
        <select name="salutation" id="salutation">
          <option disabled selected>Please pick one</option>
          <option>Mr.</option>
          <option>Mrs.</option>
          <option>Dr.</option>
          <option>Prof.</option>
          <option>Other</option>
        </select>
      </fieldset>
    </form>
    </div>