wawan Setiyawan wawan Setiyawan - 4 months ago 10
Javascript Question

selected dropdown from data json

I've a code html :



<div id='dropdown' class='dropdown'>
<select id='select_drop' class='select_drop'>
<option value=''>-select-</option>
<option value='1'>'<value>'</option>
<option value='2'>'<value>'</option>
<option value='3'>'<value>'</option>
</select>
</div>





This is data json :

{
"bahan": [{
"id_bahan": "4",
"nm_jenis": "katun"
}, {
"id_bahan": "2",
"nm_jenis": "spandex"
}, {
"id_bahan": "2",
"nm_jenis": "spandex"
}]
}


I want to fill value with data json, but I've
id_bahan
more than one.
I use
jquery
, and I add dropdown with
.clone()

Answer

Try a each loop

var obj = {
    "bahan": [{
        "id_bahan": "4",
        "nm_jenis": "katun"
    }, {
        "id_bahan": "2",
        "nm_jenis": "spandex"
    }, {
        "id_bahan": "2",
        "nm_jenis": "spandex"
    }]
};
$.each(obj.bahan,function(i,v){
   $('body').append('<select id="select_drop" class="select_drop"><option value="'+v.id_bahan+'">'+v.id_bahan+'</option></select>');
});