I have a select element on my HTML page. I want to populate it with an array. as we can give an array as dataprovider to comboBox in action script. I do the following
<This is the simplest way to populate comboBox with an array.
var j = new Array("option1","option2","option3","option4","option5"),
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i]+ '">' + j[i] + '</option>';
}
$("#rec_mode").html(options);
I would recommend to use object instead of array it will be of great helpful and in respected manner with standards. The reason is WE can index into the object by the "key" and get the "value" . To display contents and resetting them you can follow this NOTES
CHECK HERE
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<td>
<label>Recording Mode:</label>
</td>
<td>
<select id="rec_mode">
</select>
</td>
</tr>
</table>
</body>
<script>
var myobject = {
ValueA : 'Text A',
ValueB : 'Text B',
ValueC : 'Text C'
};
var select = document.getElementById("rec_mode");
for(index in myobject) {
select.options[select.options.length] = new Option(myobject[index], index);
}
</script>
</html>
This is the best way you can get array values in combo box
var states = new Array();
states['India'] = new Array('Andhra Pradesh','Arunachal Pradesh','Assam','Bihar','Chhattisgarh','Goa','Gujarat','Haryana','Himachal Pradesh','Jammu and Kashmir','Jharkhand','Karnataka','Kerala','Madhya Pradesh','Maharashtra','Manipur','Meghalaya','Mizoram','Nagaland','Odisha','Punjab','Rajasthan','Sikkim','Tamil Nadu','Telangana','Tripura','Uttar Pradesh','Uttarakhand','WestBengal','Andaman and Nicobar Islands','Chandigarh','Dadra and Nagar Haveli','Daman and Diu','Lakshadweep','Puducherry');
function setStates() {
var newOptions=states['India'];
var newValues=states['India'];
selectField = document.getElementById("state");
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++)
{
selectField.options[selectField.length] = new Option(newOptions[i], newValues[i]);
}
}
<body onload="setStates();"
<label>State :</label>
<select style="width:auto;" name="state" id="state">
<option value="">Please select a Country</option>
</select><br>
I highly recommend you use a format like the following:
var options =
[
{
"text" : "Option 1",
"value" : "Value 1"
},
{
"text" : "Option 2",
"value" : "Value 2",
"selected" : true
},
{
"text" : "Option 3",
"value" : "Value 3"
}
];
var selectBox = document.getElementById('rec_mode');
for(var i = 0, l = options.length; i < l; i++){
var option = options[i];
selectBox.options.add( new Option(option.text, option.value, option.selected) );
}
You don't run in to the problem of having to select a default option and you can easily generate the options array dynamically.
-- UPDATE --
ES6 version of the above code:
let optionList = document.getElementById('rec_mode').options;
let options = [
{
text: 'Option 1',
value: 'Value 1'
},
{
text: 'Option 2',
value: 'Value 2',
selected: true
},
{
text: 'Option 3',
value: 'Value 3'
}
];
options.forEach(option =>
optionList.add(
new Option(option.text, option.value, option.selected)
)
);
You can do this:
var videoSrcArr = new Array("option1","option2","option3","option4","option5"),
selectEl = document.getElementById('rec_mode');
for(var i = 0; i < videoSrcArr.length; i++){
selectEl.options.add(new Option(videoSrcArr[i], videoSrcArr[i]));
}