jquery - Better way to load dropdown list from comma separated string -
i have string of comma separated values. need split string , load each entry in dropdown
. have following code job.
is there better code reduces number of lines without using other library?
code
//remove existing entries dropdown $('.ddlasn').empty(); //split string array var arr = result.split(','); //loop through array for(var = 0; i<arr.length; i++) { //add ddl options - text , value $('.ddlasn').append($('<option></option>').val(arr[i]).html(arr[i])); }
you can simplify to:
$.each(result.split(','), function(i, e) { $('.ddlasn').append($('<option>', { value: e, text: e })); });
Comments
Post a Comment