Best way to create a dynamic Select (Dropdown) list?

11,669

Solution 1

Got it- a good solid 8 hours later.

var taskList = "<select name='taskList' Enabled='true' onClick='$(\"#hoursBx\").valid()' >";
for (var i = 0; i < result.TaskTypes.length; i++) {
    if (result.TaskTypes[i].TaskName == rowData.TaskType)
        taskList += "<option selected> " + result.TaskTypes[i].TaskName + "</option>";
    else
        taskList += "<option>" + result.TaskTypes[i].TaskName + "</option>";
}
taskList += "</select>";

I'm using jQuery's Validator to verify the value in the $("#hoursBx") (a text box in the current row).

Adding the onClick works.

Solution 2

The way I would have done it, is in your first example - instead of using the jQuery API for addOption, use the DOM API, like this:

var option = document.createElement("option");
option.innerHTML = result.TaskTypes[i].TaskName;
option.value = result.TaskTypes[i].TaskId;
option.onclick = myClickHandler;
taskList.add(option, null);

Then after the loop you can just use:

taskList.selectedIndex = taskIndex;

to have the select list positioned to your required default selection.

I haven't used jQuery extensively, but I think its a good idea not to neglect the DOM API - its often not as convenient as the shortcuts that jQuery and other libraries offer, but these extend DOM capabilities and should not come instead of the DOM.

Solution 3

Falling under the "better way" category, JQuery lets you use an each loop instead of creating the for loops manually.

jQuery.each(result.TaskTypes, function(i, val) {
      $("#" + i).append(document.createTextNode(" - " + val));
    });

Solution 4

You can set the selected index like this:

$("#taskList").selectedIndex = taskIndex;
Share:
11,669
Brad8118
Author by

Brad8118

I graduated from RIT (Rochester Institute of Technology) in 2006 with a computer science degree. While at RIT I played for the Men's Varsity Soccer Team. I'm currently working for InfoDirections, which is located in Rochester NY. I've only been working here for about a month now and it seems to be a very friendly and enjoyable place to work. The company strives to be developed around its employees. No one is pulling out their hair and the company has lots of events to keep moral high. Info Dirs develops software for telecommunications billing solutions. I'm learning what this really means everyday. The create a software portal when one can logon and check their landline/mobile bill, add new features, pay online and more. We also create the app that creates each user profile, the phone plan, calculate the taxes, manage how to bill when out of network and much much more.

Updated on June 05, 2022

Comments

  • Brad8118
    Brad8118 about 2 years

    I'm using jQuery and jqGrid.

    I'm trying to populate a select list dynamically, one for each row and I need to add a click event to it. When the select list is being populated I grab the index of the item I want selected, and then after all items are add I'm trying to set the selected item.

    I've tried

    $("#taskList")[0].selectedIndex = taskIndex;
    $("#taskList").selectOptions(taskIndex, true);
    $("#taskList").val(1); //Tried to see if I could select any index and no luck.
    $("#taskList option[value=" + taskIndex + "]").attr("selected", true);
    

    So this means I'm probably populating the list incorrectly...

    var taskList = document.createElement("select");
    var taskIndex = 0;
    for (var i = 0; i < result.TaskTypes.length; i++) {
       $(taskList).addOption(result.TaskTypes[i].TaskId, result.TaskTypes[i].TaskName);
       if (result.TaskTypes[i].TaskName == rowData.TaskType)
        taskIndex = i;
    }
    

    Is there a better way?

    I tried this but I couldn't add the click event to it. The proper item was selected though.

    var taskList = "<select name='taskList' Enabled='true'>";
    for (var i = 0; i < result.TaskTypes.length; i++) {
        if (result.TaskTypes[i].TaskName == rowData.TaskType)
            taskList += "<option selected> " + result.TaskTypes[i].TaskName + "</option>";
        else
        taskList += "<option>" + result.TaskTypes[i].TaskName + "</option>";
    }
    taskList += "</select>";