Posted By

Neven on 12/12/11


Tagged

select text Fill values DropDownList pair


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

BrockSamsom


Fill select list with text value pairs


 / 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.

  1. function FillSelectHtmlElement(ctrlId,result) {
  2. $(ctrlId).get(0).options.length = 0;
  3. $(ctrlId).get(0).options[0] = new Option("-- Make your selection --", "-1");
  4. //$('<option/>').appendTo(ctrlId); // uncomment this line if you wish an empty select option
  5.  
  6. $.each(result, function (index, item) {
  7. $(ctrlId).get(0).options[$(ctrlId).get(0).options.length] = new Option(item.Text, item.Value);
  8. });
  9. }
  10.  
  11. Call this function like this:
  12. var ddl = $("#myDropDownList");
  13. var someTestValues = [{ "Text": "Selection 1", "Value": "Value1" }, { "Text": "Selection 2", "Value": "Value2"}];
  14. FillSelectHtmlElement(ddl,someTestValues );

Report this snippet  

You need to login to post a comment.