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 ??.
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 ??.
Share Improve this question asked Mar 26, 2013 at 22:07 erdomestererdomester 11.8k33 gold badges135 silver badges239 bronze badges2 Answers
Reset to default 6var 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.
There you go:
var year = getYear();
var bo = document.getElementById('dateformat');
bo.options[bo.options.length] = new Option(year ,year );