Reputation: 85
I'm trying to change the values of an existing select from Javascript, and after a while looking for in internet I am not sure that is possible.
I have this piece of code:
function clickPic(pic_select, choosen, opt){
//Here I change the border of a selected image
if (opt!=0){
pic_select.style.border = "3px solid blue";
}else{pic_select.style.border = "3px solid red";}
// here I send the variable (it's a number) which I want to change the values of the select.
var option_master = opt;
cambiarSelect(option_master);
}
function cambiarSelect(num){
//here num is 4 or 6 or 12, depending
//this is the select itself
var people = document.getElementById('people');
//I guess here I reset the select
people.options.length=0;
for(var i=1;i<=num.length;i++){
//here I want to change the values of the select with the same number than num, because thats the value I'm interested to select later
people.options[i]=new Option(i);
}
}
I'm sorry for my way of programming but it's been a long time since I don't touch a keyboard, but I kind of remember it was possible some how
Upvotes: 0
Views: 245
Reputation: 33736
function cambiarSelect(num) {
//here num is 4 or 6 or 12, depending
//this is the select itself
var people = document.getElementById('people');
//I guess here I reset the select
people.innerHTML = ''; //Clean your select
for (var j = 1; j <= num; j++) {
//here I want to change the values of the select with the same number than num, because thats the value I'm interested to select later
people.add(new Option(j, j));
}
}
cambiarSelect(6);
<select id='people'>
<option>Temporary</option>
<option>Temporary2</option>
<option>Temporary3</option>
</select>
Upvotes: 3