I want add options to the select with a js function. But there is a problem on the vector, i think, someone can help me?
<html>
<body onload="myFunction()">
<select id="mySelect"></select>
<script>
function myFunction() {
var months= ["january","february"];
var mySelect = document.getElementById('mySelect');
var newOption = document.createElement('option');
for(var i=0;i<mesi.length;i++){
newOption.innerHtml=mesi(i).valueOf;
mySelect.appendChild(newOption);
}
}
</script>
</body>
</html>
Your code seems to have the wrong array name in the for block. Also, when you're calling an index in an array - use square brackets - months[i]
, not parentheses. And using valueOf is unnecessary.
function myFunction() {
var months= ["january","february"];
var mySelect = document.getElementById('mySelect');
var newOption = document.createElement('option');
for(var i=0;i<months.length;i++){ // <- here is where the fix goes.
newOption.innerHtml=months[i];// <- here is where the fix goes.
mySelect.appendChild(newOption);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.