How do I set the value of input element type month with javascript?
<input type="month>
Couldn't find anything about that in the web.
You can use the value attribute like any other type
<input type="month" value="2017-09">
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month
If you are looking for JS solution, just like you set value to an input, you can do
document.getElementById("abc").value = "2017-09";
Notes:
1) While using it, there must be always year. This is a major annoying thing.
2)Be aware that there are browser issues with it.
However, there are issues with because at this time, many major browsers don't yet support it.
We'll look at basic and more complex uses of , then offer advice on mitigating the browser support issue in the section Handling browser support).
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month#Handling_browser_support
Suggestion :
I personally don't like the way it is developed and designed in the current want docs supporting the same
The best way to deal with dates in forms in a cross-browser way (until all of the major browsers have supported them for a while) is to get the user to enter the month and year in separate controls ( elements being popular; see below for an implementation), or use JavaScript libraries such as the jQuery date picker plugin.
Your can set value as below,
const monthControl = document.querySelector('#month-control[type="month"]');
monthControl.value = '1978-06';
<input type="month" id="month-control">
Please consider the Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari
Basic support | 20 | 12 | No support[1] | No support | 10.62 | No support[2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With