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 ??.
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.
There you go:
var year = getYear();
var combo = document.getElementById('dateformat');
combo.options[combo.options.length] = new Option(year ,year );
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.