Hello everyone.
I'm currently learning Javascript and doing a few exercises.
One problem I'm working on takes an array of names from an xml file using Ajax and writes it to <select> <options> tags.
This is the code they use:
Code:
function writeSelect (name_group) {
var this_option;
var my_select = document.getElementById("namelist");
for (var loop = 0; loop < name_group.length; loop++)
{
this_option = new Option( );
this_option.value = the_array[loop];
this_option.text = the_array[loop]
my_select.options[loop] = this_option;
}
}
The parameter for writeSelect() function contains the array of names and the id "namelist" is the id for the select tag in the form element.
This is my first time seeing the new Option() function. Is this the standard for writing arrays to the <select> tag? Or could this have been done another way - if so what this that way?
Any response would be appreciated.
|