1

I have a drop down list(select list) with ID TestList.

I want to use this as selectmenu from jQuery UI. This drop down is getting dynamically populated so initially there are no options to this select list. In the scripts section, I am first initializing the select list with select menu as:

$("#TestList").selectmenu();

Next, I want to dynamically populate it with data, so I am doing this:

for (var i = 0; i < data.length; i++) {           
   $('#TestList').val(data[i]);
    $("#TestList").selectmenu("refresh");
} 

But the problem is, it throws Uncaught Error: no such method 'instance' for menu widget instance.

I followed How to select an option in a jQuery ui selectmenu dynamically? but the problem still persists. I am using the latest jQuery UI package(jquery-ui-1.12.1.custom) and jQuery 1.9 Please help me solve this issue.. List:

enter image description here

Del Monte
  • 153
  • 3
  • 14
  • In your source code can you please provide the array for `data`? And do you want select the option with value `refresh`? – Rahul Gupta Dec 28 '17 at 09:03
  • Are you asking the potential values of `data`? If so, then the array would contain values of color like 'red', 'blue', 'green' etc. Also, yes I want to store selected value in DB hence I am using `refresh`. – Del Monte Dec 28 '17 at 09:17

1 Answers1

1

I have created a DEMO.

This DEMO:

  1. Dynamically initializes the selectmenu widget on the HTML dropdown
  2. Dynamically create/load options
  3. Dynamically select a specific option in the dropdown

I think it would of great help to you!

Here is the complete code from the DEMO:

<script>
  $( function() {
    $('#initialize').click(function() {
        $( "#speed" ).selectmenu();
      $('#status').html('The widget is now initialized. But it is still empty and does not contain any options.');
      $('#add_options').removeAttr('disabled');
      $('#initialize').attr('disabled','');
    });


        $('#add_options').click(function() {
        // Add a new option to the dropdown
        $('#speed').append($('<option>', {
          value: 1,
          text: 'Option 1'
      }));
      $('#speed').append($('<option>', {
          value: 2,
          text: 'Option 2'
      }));
      $('#speed').append($('<option>', {
          value: 3,
          text: 'Option 3'
      }));
      //Refresh the selectmenu widget so the newly added options to the dropdown are now loaded properly
      $( '#speed' ).selectmenu( "refresh" );

      $('#status').html('The new options are now added dynamically!!');
      $('#select_option').removeAttr('disabled');
      $('#add_options').attr('disabled','');
    });

    $('#select_option').click(function() {
        $( '#speed' ).val(3);
      $( '#speed' ).selectmenu( "refresh" );

      $('#status').html('Options 3 is now selected!!');
      $('#select_option').attr('disabled','');
    }); 
  });
  </script>

 <div class="demo">
 <input type="button" value="Initialize the Widget" id="initialize"/>
 <br><br>
 <input type="button" value="Dynamically Add New Options" id="add_options" disabled/>
 <br><br>
 <input type="button" value="Select the 'Option 3'" id="select_option" disabled/>
<form action="#">
  <br>
  <div id="status" style="font-weight:bold;">This is the simple default HTML dropdown</div>
  <br>
    <label for="speed">Select a speed</label>
    <select name="speed" id="speed">
    </select>

</form>

</div>
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
  • Brilliant work done there mate! But I am still getting `Uncaught Error: no such method 'instance' for menu widget instance.` on page load i.e even before populating the list the error is shown on the console. I am using jQuery 1.9. Is this the reason? – Del Monte Dec 28 '17 at 10:19
  • I guess it was because I had written inside document.ready function so it seems to have been solved :) – Del Monte Dec 28 '17 at 10:32
  • Glad to help you :) – Rahul Gupta Dec 28 '17 at 10:40
  • The select list looks weird. It's height and width are not proper.. This is originally a bootstrap select list. Is this the reason for such deformity? – Del Monte Dec 28 '17 at 10:42
  • 1
    If you would use multiple jquery plugins to manipulate the HTML elements then they would surely cause layout issues because the css rules would conflict – Rahul Gupta Dec 28 '17 at 10:44