Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQueryUI selectmenu - How to add more options

Through javascript how can I add more options to a dropdown selectmenu?

Currently trying the following with no luck:

for (i = 0; i < json.powerDropDownItems.length; i++) {
    //$('#powerSelect').append($("<option></option>").attr("value", json.powerDropDownItems[i]).text(json.powerDropDownItems[i]));
    $('#powerSelect').selectmenu("value", "nice name");
    //$('#powerSelect').appendTo("<option>" + json.powerDropDownItems[i] + "</option>");
}
$('#powerSelect').selectmenu("refresh");​

UPDATE

Thanks to naveen, I got it working (also added code to clear the list). Here is my following code:

 service.getPowerDropDowns(productEC, $('#mountSelect').val(), function (response) {
       var json = $.parseJSON(response.value);

       var options = [];

       // Clear the options first   
       $("#powerSelect option").each(function(index, option) {
            $(option).remove();
       });
        options.push("<option value=''>Choose</option>");
        for (i = 0; i < json.powerDropDownItems.length; i ++)
        {
            options.push("<option value='" + json.powerDropDownItems[i] + "'>" + json.powerDropDownItems[i] + "</option>");
        }
        $('#powerSelect').append(options.join("")).selectmenu();
        $('#powerSelect').selectmenu('enable');
    });
like image 408
Channafow Avatar asked Sep 05 '12 00:09

Channafow


2 Answers

This will work

$(function() {
    var options = []; 
    for (i = 0; i < json.powerDropDownItems.length; i++) {
        options.push("<option value='" + json.powerDropDownItems[i] + "'>" + json.powerDropDownItems[i] + "</option>");
    }
    //append after populating all options
    $('#powerSelect')
        .append(options.join(""))
        .selectmenu();
});​

Demo: http://jsfiddle.net/codovations/p863Q/

like image 113
naveen Avatar answered Nov 06 '22 02:11

naveen


Depends on the version.

https://github.com/jquery/jquery-ui/tree/selectmenu uses refresh method

https://github.com/fnagel/jquery-ui/tree/selectmenu uses selectmenu() like naveen described.

like image 38
fnagel Avatar answered Nov 06 '22 02:11

fnagel