javascript - How to generate option values into jquery multiselect from js -
i trying generate dynamically option values in jquery multiselect using javascript.
function prova(data){ var t = 0; var tmp = new array(); var keys = new array(); $("#select-1").multiselect({ multiple: true, header: true, noneselectedtext: "select option", selectedlist: 1 }); var stringa =""; $.each(data, function(key,value){ stringa +="<option value=\""+key+"\">"+key+"</option>"; if(t==0){ tmp[0] = key; } t++; }); $("#select-1").html(stringa); }
my values generated in #select-1 that:
<select id="select-1" size="5" name="example-basic" title="basic example" style="display: none;"> <option value="name1">name1</option> <option value="name2">name2</option> <option value="name3">name3</option> <option value="name4">name4</option> <option value="name5">name5</option> </select>
the problem doesn't fill option values jquery multiselect, remains empty.
because using .html() directly on element, replacing elements inside before inserting new elements. might want use .append() like:
$.each(data, function(key,value){ stringa +="<option value=\""+key+"\">"+key+"</option>"; if(t==0){ tmp[0] = key; } t++; }); $("#select-1").append(stringa);
or use previous values well, in .html() usage like:
$.each(data, function(key,value){ stringa +="<option value=\""+key+"\">"+key+"</option>"; if(t==0){ tmp[0] = key; } t++; }); $("#select-1").html($("#select-1").html()+stringa);
Comments
Post a Comment