HTML | DOM Input Month Object - GeeksforGeeks

14 Feb, 2022
The Input Month Object in HTML DOM is used to represent an HTML input element with type= “month” attribute. The input element with type= “month” attribute can be accessed by using getElementById() method.
Syntax:  
It is used to access <input> element with type=”month” attribute.
document.getElementById("id");
It is used to create <input> element with type=”month” attribute.
document.createElement("input");
Property Values: 
Input Month Object Methods:  
Example 1: This example use getElementById() method to access <input> element with type=”month” attribute.  
html
<!DOCTYPE html><html> <head>    <title>        HTML DOM Input Month Object    </title></head> <body>     <h1>GeeksforGeeks</h1>     <h2>DOM Input Month Object</h2>     <input type = "month" id = "month" value = "2018-02">     <button onclick = "myGeeks()">Click Here!</button>         <p id = "GFG"></p>           <!-- script to access input element of        type month attribute -->    <script>        function myGeeks() {            var val = document.getElementById("month").value;            document.getElementById("GFG").innerHTML = val;        }    </script></body> </html>                   
Output: Before click on the button:  
After click on the button:  
Example 2: This example use document.createElement() method to create <input> element with type=”month” attribute.  
html
<!DOCTYPE html><html> <head>    <title>        HTML DOM Input Month Object    </title></head> <body>     <h1>GeeksforGeeks</h1>     <h2>DOM Input Month Object</h2>     <button onclick = "myGeeks()">Click Here!</button>         <!-- script to create input element of        type month attribute -->    <script>        function myGeeks() {                         /* Create an input element */            var x = document.createElement("INPUT");                         /* Set the type attribute */            x.setAttribute("type", "month");                         /* Set the value to type attribute */            x.setAttribute("value", "2018-02");                         /* Append the element to body tag */            document.body.appendChild(x);        }    </script></body> </html>                   
Output: Before click on the button:  
After click on the button:  
Supported Browsers:
Google Chrome
Internet Explorer (after IE 11)
Opera
Safari
 
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
ManasChhabra2
hritikbhatnagar2182
HTML-DOM
Picked
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to set the default value for an HTML <select> element ?
How to update Node.js and NPM to next version ?
How to set input type date in dd-mm-yyyy format using HTML ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?