I am trying to add the same list of names to 10 different select forms in html via javascript. While this code works, it adds the list of names to each box 10 times. However, if I don't have the q loop, then nothing gets added at all.
I don't really know what I'm doing wrong, but I just don't want the same list of names to be added 10 times. Any ideas?
Thanks!
for(i = 0; i < 10; i++){
var names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
//window.alert(names[i])
document.write('<tr>');
document.write('<td>');
document.write('Enter name '+ names[i]);
document.write('</td>');
document.write('<td>');
document.write('<select name=' + i + ' id=' + names[i] + '></select>');
var textFile = "/names.txt";
jQuery.get(textFile, function(textFileData) {
var EachName= textFileData.split("\n");
for (q = 0; q < 10; q++) {
var select = document.getElementById(names[q]);
for (var j = 0, len = EachName.length; j < len; j++) {
var option = document.createElement('option');
option.text = option.value = EachName[j];
select.add(option, 0);
}
}
});
document.write('</td>');
document.write('</tr>');
};