Could not find anything abou...">

Setting the month of input

How to set month value of input element type using javascript?

<input type="month>

Could not find anything about this on the Internet.

+4
source share
4 answers

You can use the value attribute like any other type

    <input  type="month"  value="2017-09">
Run codeHide result

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month


If you are looking for a JS solution, just as you set the value for input, you can do

document.getElementById("abc").value = "2017-09";

Notes:

1) When using it, there should always be a year. This is a big nasty thing.

2) Keep in mind that there are problems with the browser with it.

However, there are problems, because at this time many major browsers do not yet support it.

, " " ).

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month#Handling_browser_support


:

, ,

- ( - ) , ( , . ) JavaScript, jQuery.

+4

, ,

const monthControl = document.querySelector('#month-control[type="month"]');
monthControl.value = '1978-06';
<input type="month" id="month-control">
Hide result

,

Feature       | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari
Basic support | 20     | 12   | No support[1]   | No support        | 10.62 | No support[2]
+3

As Suresh said, you can access and update it, like any other type of input, this way:

<input type="month" id="month_input">

And in your JS:

document.getElementById("month_input").value = "2017-09";
+1
source

var month = document.getElementsByClassName('month');

for (i = 0; i < month.length; i++) {
   month[i].value = '2017-06';
} 
<input type="text" class="month"  >
<input type="text" class="month"  >
Run codeHide result
you can also use class
+1
source

Source: https://habr.com/ru/post/1689004/


All Articles