Reputation: 11829
How can I sett the value and text of a select option from javascript?
function getYear()
{
var d = new Date();
var year = d.getFullYear();
return year;
}
<select name="dateformat" onchange="change_date();" id="dateformat">
<option value="??" >??</option>
</select>
I want the value of getYear() in place of ??.
Upvotes: 3
Views: 8485
Reputation: 22617
var option = document.getElementById("dateformat").options[0];
option.value = option.text = getYear();
Edit: I thought OP meant to find a way to add an option to a <select>
. But it seems it's more like editing an existing one. So there.
Upvotes: 6
Reputation: 5253
There you go:
var year = getYear();
var combo = document.getElementById('dateformat');
combo.options[combo.options.length] = new Option(year ,year );
Upvotes: 1