0

I got these data return from PHP script in JSON format.

{
"inherited_items":{1:"Role Num1",2:"Role Num2",3:"Role Num3"},
"available_items":{4:"Role Num4",6:"Role Num5",6:"Role Num6"}
}

How do I set this values as options to my select input? I came to this question " Jquery getJSON populate select menu question", but the solution will not work for me. It is because the JSON data I have does not have a uniform index to the array values. Is there a way to set the data as option values with out reformatting the JSON data from the PHP script. As the index of the arrays are important to me.

By the way, the JSON data that I have will be used to populate to two different select input.

Community
  • 1
  • 1
Pelang
  • 421
  • 7
  • 19
  • You have two objects here `inherited_items` and `available_items` which one do you want to use – Arun P Johny May 20 '13 at 03:45
  • @ArunPJohny Both of them will be used to populate the option value of two select input. inherited_items data will be used for one select input and available_items for another select input. Thanks for the reply. – Pelang May 20 '13 at 03:49
  • You have to handl indexes that are having the same values in them like Role number 5 and Role num 6 with Index Number 6. If you loop this, the Role Num5 will be replaced with Role Num6. For hard core js, you can populate the select with http://jsfiddle.net/KDBJt/ – vincent May 20 '13 at 04:02

1 Answers1

2

To populate a select element with id test from data inherited_items

$.each(data.inherited_items, function(key, value){
    $('<option />', {
        value: key,
        text: value
    }).appendTo('#test')
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531