Return to Snippet

Revision: 54149
at December 12, 2011 21:37 by Neven


Initial Code
function FillSelectHtmlElement(ctrlId,result) {
        $(ctrlId).get(0).options.length = 0;
        $(ctrlId).get(0).options[0] = new Option("-- Make your selection --", "-1");
        //$('<option/>').appendTo(ctrlId); // uncomment this line if you wish an empty select option

        $.each(result, function (index, item) {
            $(ctrlId).get(0).options[$(ctrlId).get(0).options.length] = new Option(item.Text, item.Value);
        });
    }

Call this function like this:
var ddl = $("#myDropDownList");
var someTestValues = [{ "Text": "Selection 1", "Value": "Value1" }, { "Text": "Selection 2", "Value": "Value2"}];
FillSelectHtmlElement(ddl,someTestValues );

Initial URL


Initial Description
This snippet describes how to fill select html element with Text, Value pairs
where 'Text' is displayed to the user and 'Value' is actual value to be processed.

Initial Title
Fill select list with text value pairs

Initial Tags
text

Initial Language
jQuery