If I have a dropdownlist like so:
How do I set the default value to equal today's current month-year via js?
Answer
You could do something like this.
var d = new Date();
var m = d.getMonth() + 1;
var str = d.getFullYear() + "-" + (m >= 10 ? "" : "0") + m + "-01T00:00:00";
var select = document.getElementById("dropdown");
var options = select.options;
for(var i = 0; i < options.length; i++){
if(options[i].value == str){
select.selectedIndex = i;
break;
}
}
No comments:
Post a Comment