/ Published in: jQuery
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.
where 'Text' is displayed to the user and 'Value' is actual value to be processed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 );