Javascript Reference - HTML DOM Input Month value Property








The value property sets or gets the value attribute of a month field.

The value attribute sets a month and year for the month field.

Browser Support

value Yes No No Yes Yes

Syntax

Return the value property.

var v = monthObject.value

Set the value property.

monthObject.value=YYYY-MM





Property Values

Value Description
YYYY-MM Set the month and year.
  • YYYY - year (e.g. 2011)
  • MM - month (e.g. 01 for January)

Return Value

A String type value, representing the month and year of the month field.

Example

The following code shows how to get the month and year of a month field.


<!DOCTYPE html>
<html>
<body>
Month and year: <input type="month" id="myMonth" value="2014-11">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- w w  w  .j  ava  2 s .  co  m-->
<script>
function myFunction() {
    var x = document.getElementById("myMonth").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to set the month and year for a month field.


<!DOCTYPE html>
<html>
<body>
Month and year: <input type="month" id="myMonth">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   w w  w  . java  2 s .  c  o m-->
    document.getElementById("myMonth").value = "2014-02";
}
</script>

</body>
</html>

The code above is rendered as follows: