Dil.
Dil.

Reputation: 2076

How to populate drop down using two dimension array in javascript


I'm trying to create two dimensional array and populate a dropdown using it.
But I'm getting Uncaught SyntaxError: Unexpected string error.

var cuisines = ["001","Australian"],["002","American"];     

var sel = document.getElementById('CuisineList');
for(var i = 0; i < cuisines.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = cuisines[i][1];
    opt.value = cuisines[i][0];
    sel.appendChild(opt);
}
<select id="CuisineList"></select>


Please help me. Thanks.

Upvotes: 2

Views: 535

Answers (3)

berko
berko

Reputation: 383

Change var cuisines = ["001","Australian"],["002","American"]; to var cuisines =[ ["001","Australian"],["002","American"]]; so you actually have a two dimensional array, and change your [o] to [0].

Upvotes: 2

Matthew Woodard
Matthew Woodard

Reputation: 96

You need outer brackets when defining your two dimensional array.

var cuisines = [["001","Australian"],["002","American"]];

Also, looks like you're using the letter "o" instead of the number 0 for one of your indexes.

Upvotes: 4

asdf_enel_hak
asdf_enel_hak

Reputation: 7630

I did not investigate your code deeply but you have error:

 opt.value = cuisines[i][o];

should be

 opt.value = cuisines[i][0];

you are using "o" letter instead of 0 (zero)

Upvotes: 2

Related Questions