Você pode conseguir isso com um forloop simples :
var min = 12,
max = 100,
select = document.getElementById('selectElementId');
for (var i = min; i<=max; i++){
var opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
select.appendChild(opt);
}
Demonstração JS Fiddle .
A comparação JS Perf da resposta da minha e da Sime Vidas foi executada porque achei que a dele parecia um pouco mais compreensível / intuitiva do que a minha e fiquei imaginando como isso se traduziria em implementação. De acordo com o Chromium 14 / Ubuntu 11.04, a mina é um pouco mais rápida, mas é provável que outros navegadores / plataformas tenham resultados diferentes.
Editado em resposta ao comentário do OP:
[Como] aplico isso a mais de um elemento?
function populateSelect(target, min, max){
if (!target){
return false;
}
else {
var min = min || 0,
max = max || min + 100;
select = document.getElementById(target);
for (var i = min; i<=max; i++){
var opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
select.appendChild(opt);
}
}
}
// calling the function with all three values:
populateSelect('selectElementId',12,100);
// calling the function with only the 'id' ('min' and 'max' are set to defaults):
populateSelect('anotherSelect');
// calling the function with the 'id' and the 'min' (the 'max' is set to default):
populateSelect('moreSelects', 50);
Demonstração JS Fiddle .
E, finalmente (depois de um atraso ...), uma abordagem estendendo o protótipo do HTMLSelectElementpara encadear a populate()função, como método, para o nó DOM:
HTMLSelectElement.prototype.populate = function (opts) {
var settings = {};
settings.min = 0;
settings.max = settings.min + 100;
for (var userOpt in opts) {
if (opts.hasOwnProperty(userOpt)) {
settings[userOpt] = opts[userOpt];
}
}
for (var i = settings.min; i <= settings.max; i++) {
this.appendChild(new Option(i, i));
}
};
document.getElementById('selectElementId').populate({
'min': 12,
'max': 40
});
Demonstração JS Fiddle .
Referências: