Javascript Reference - HTML DOM Input Hidden value Property








The value property sets or gets the value attribute of the hidden input field.

The value attribute defines the default value of the hidden input field.

Browser Support

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = hiddenObject.value 

Set the value property.

hiddenObject.value=text




Property Values

Value Description
text Set the value of the input field

Return Value

A String type value representing the value attribute of the hidden input field.

Example

The following code shows how to change the value of the hidden field.


<!DOCTYPE html>
<html>
<body>
<!--from  ww w. ja  va 2 s. co m-->
A hidden input field:
<input type="hidden" id="myInput" name="country" value="USA">
<button onclick="display()">Display value</button>
<button onclick="change()">Change value</button>

<script>
function display() {
    var x = document.getElementById("myInput").value;
    console.log("The value: " + x);
}

function change() {
    var x = document.getElementById("myInput").value="USA";
    console.log("The value was changed to: " + x);
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the value attribute of a hidden input field.


<!DOCTYPE html>
<html>
<body>
<input type="hidden" id="myInput" value="abc">
<!--from   www.  j  av a2 s  .c  o m-->
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: