Tuesday 10 September 2019

javascript - Set Default Value in Dropdownlist based on current date



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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...