I am facing a challenge with a large JavaScript array that contains about 1000 student names. Each element in the array consists of a student's ID number as its key and the student's name as its value. Here is an example:
student_array['2312'] = 'Bloggs, Joe';
student_array['332'] = 'Carter, John';
student_array['9423'] = 'Davies, David';
The array is arranged in order of the array value. I then create a select box by iterating over the array and generating an option for each value. The key from the array serves as the option value, while the array value becomes the option text.
This is the snippet of my JavaScript code used to create the select box:
var student_select = '<select name="student_id" id="student_id">';
for(x in students_array)
{
student_select += '<option ' + (student_id==x ? 'selected="selected"' : '') + ' value="' + x + '">' + students_array[x] + '</option>';
}
student_select += '</select>';
In Internet Explorer 8, everything works fine, and the options in the select box are sorted by the value. However, in Chrome, the options are ordered by the key instead, which makes searching for a specific student name challenging.
Question: Is there a way to make Chrome order the select box options by array value rather than by the key?