I'm a student in JavaScript and we have an assignment due tonight. I've been working, and re-working, the code for bit.
Essentially, I'm struggling with populating a List Box (bo box) from a Multi-Dimensional Array.
I have a multi-dimensional array created and it works (I sent my results to an alert box).
var concertArray = [
["Billy Joel", "99", "equal.png"],
["Bryan Adams", "89", "higher.png"],
["Brian Adams", "25", "lower.png"]
];
But, I have no idea how to populate this array to a select list box (bo box).
Any help, much appreciated.
I'm a student in JavaScript and we have an assignment due tonight. I've been working, and re-working, the code for bit.
Essentially, I'm struggling with populating a List Box (bo box) from a Multi-Dimensional Array.
I have a multi-dimensional array created and it works (I sent my results to an alert box).
var concertArray = [
["Billy Joel", "99", "equal.png"],
["Bryan Adams", "89", "higher.png"],
["Brian Adams", "25", "lower.png"]
];
But, I have no idea how to populate this array to a select list box (bo box).
Any help, much appreciated.
for
loop?
– Matt Ball
Commented
Apr 22, 2012 at 12:42
Use this Code.
<html>
<head>
<script type="text/javascript">
var concertArray = [
["Billy Joel", "99", "equal.png"],
["Bryan Adams", "89", "higher.png"],
["Brian Adams", "25", "lower.png"]
];
function populate(){
for(i=0;i<concertArray.length;i++){
var select = document.getElementById("test");
select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]);
}
}
</script>
</head>
<body onload="populate();">
<select id="test">
</select>
</body>
</html>
this will help you.....
extract the data which you want to add in bo box from the array, then use jquery append
to add them to the select tag.
<select id="test">
</select>
Iterate through the array & store the data to be added in a variable, say var data, then:
$('#test').append('<option value="'+data+'">'+data+'</option');