0

i have json data like this:

[{
        "user_id": "113",
        "employe_first_name": "Asaladauangkitamakan",
        "employe_last_name": "Nasibb"
    }, {
        "user_id": "105",
        "employe_first_name": "Ryan",
        "employe_last_name": "Friday"
    }, {
        "user_id": "87",
        "employe_first_name ":"hendi ",
        "employe_last_name ":"kenther"
    }
]

how to 'create' select option with javascript (option value = user_id, and text = employe_first_name) that load when document ready, and fill another field with selected value

Hahm
  • 45
  • 5

4 Answers4

1
var data = [ {"user_id":"113","employe_first_name":"Asaladauangkitamakan","employe_last_name":"Nasibb"}, {"user_id":"105","employe_first_name":"Ryan","employe_last_name":"Friday"},   {"user_id":"87","employe_first_name":"hendi","employe_last_name":"kenther"} ];
var select = document.createElement("select");
for(var i = 0; i < data.length; i++){
    var option = document.createElement("option");
    option.value = data[i].user_id;
    option.innerHTML = data[i].employe_first_name;
    select.appendChild(option);
}
select.addEventListenter("change", function(event) {
    var selected_value = event.target.value;
    document.getElementById("another_field").value = selected_value;
});
someDiv.appendChild(select); // Add the select menu to the DOM
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try this:

var data = [ {"user_id":"113","employe_first_name":"Asaladauangkitamakan","employe_last_name":"Nasibb"}, {"user_id":"105","employe_first_name":"Ryan","employe_last_name":"Friday"},   {"user_id":"87","employe_first_name":"hendi","employe_last_name":"kenther"} ];
var select = document.createElement("select");
    for(var i = 0; i < data.length; i++){
        var option = document.createElement("option");
        option.value = data[i].user_id;
        option.innerHTML = data[i].employe_first_name;
        select.appendChild(option);
    }

Here is DEMO

Ringo
  • 3,795
  • 3
  • 22
  • 37
0

try something like this

    //doing on dom ready
    var tid = setInterval( function () {
        if ( document.readyState !== 'complete' ) return;
        clearInterval( tid );       
        var json_len = json.length;
        var select = document.getElementById('select_id');
        for(var i=0;i<json_len;i++){
            var option = document.createElement('option');
            option.innerHTML = json[i].employe_first_name;
            option.value = json[i].user_id;
            select.appendChild(option);
        }
    }, 100 );

REFERENCE

Is there a way to check document.ready() if jQuery is not available?

Community
  • 1
  • 1
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
-1

You can use jQuery for the purpose, in this way .

Replace selectId with the id of your dropdown.

 var jsonData = [{
        "user_id": "113",
        "employe_first_name": "Asaladauangkitamakan",
        "employe_last_name": "Nasibb"
    }, {
        "user_id": "105",
        "employe_first_name": "Ryan",
        "employe_last_name": "Friday"
    }, {
        "user_id": "87",
        "employe_first_name": "hendi ",
        "employe_last_name": "kenther"
    }
];
    var ctrlSelect = $("#selectId");
    $(jsonData).each(function() {
        $("<option value='" + this.user_id + "'>" + this.employe_first_name + "</option>").appendTo($(ctrlSelect));
    });
Priyank
  • 1,353
  • 9
  • 13
  • Isn't it obvious? Who said anything about jQuery? – Shea Dec 24 '13 at 20:59
  • @Shea JQuery is not a different language. It is a javascript library.So one can use JQuery for any javascript task.That is why the answer is relevant here. – Priyank Dec 25 '13 at 06:17