Definition and Usage
The type="hidden" attribute of the <input> tag represents a hidden input field containing a value that is not intended to be examined or manipulated by the user.
The value of this input field is used by web developers handling data to include additional data in a web form (<form>) without displaying it to the user.
Basic Example
<form>
<div>
<label for="title">Post Title</label>
<input type="text" id="title" name="title" value="Enjoy Learning HTML">
</div>
<div>
<label for="content">Post Content</label>
<textarea id="content" cols="21" rows="5" name="content">
To enjoy learning HTML....
</textarea>
</div>
<button type="submit">Update Post</button>
<!-- A hidden field representing a value that is not intended
to be examined or manipulated by the user -->
<input type="hidden" name="nonce" value="796c7766b1"> <!-- Used as a security token -->
</form>
Code Explanation
The <textarea> tag represents a multi-line text input field.
Common Uses
The type="hidden" attribute is primarily used by web developers handling data for various purposes, including:
Preventing User Input
It is used to prevent users from changing or entering specific values.
For example, developers can store values such as unique form identifiers or security tokens in an <input> tag specified with type="hidden" without displaying them on the screen to the user.
<form>
<label for="user-password">User Password</label>
<input type="password" id="user-password">
<button type="submit">Submit</button>
<!-- Used as a security token -->
<input type="hidden" name="nonce" value="796c7766b1">
</form>
Sending Additional Data
It is used to include additional data in a form that the user did not enter directly. For example, values such as the current page URL or user location information can be stored in a type="hidden" input field.
<form method="get" action="/search" role="search">
<label for="user-search">Site Search</label>
<input
type="search"
id="user-search"
name="query">
<button type="submit">Search</button>
<!-- Used for sending additional data: user location information -->
<input type="hidden" name="user-locale" value="seoul">
</form>
Controlling Form Logic
It is used to control the behavior of a form. For example, the value of a type="hidden" input field can be used to determine which action to perform when the form is submitted.
Caveats
Although type="hidden" represents a value that users cannot see or directly modify, it does not make the value inherently secure.
Values can be viewed and edited using the browser's developer tools, which can create a security vulnerability. While type="hidden" input fields are very useful for including additional data in a web form, take care not to store sensitive information and always implement appropriate security measures.
Using Related Attributes
The <input type="hidden"> element primarily uses the value and name attributes. These attributes play a key role in sending and processing form data.
value Attribute
The value attribute specifies the initial value of the element when the page loads.
When the form is submitted, this value can be sent to the server, or it can be included as the value of the value attribute when sending data asynchronously to the server using JavaScript's XMLHttpRequest or fetch API.
<form action="/submit" method="post">
<input type="hidden" name="user_id" value="12345">
<button type="submit">Submit</button>
</form>
XMLHttpRequest or fetch API
<input type="hidden" id="user_id" name="user_id" value="12345">
<button id="submit-button">Submit</button>
<script>
document.getElementById("submit-button").addEventListener("click", function() {
const userId = document.getElementById("user_id").value;
fetch("/submit", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `user_id=${userId}`
}).then(response => {
return response.json();
}).then(data => {
console.log(data);
});
});
</script>
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
<input type="hidden" name="session_token" value="abcdefg">
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="hidden"> |
1 | 12 | 1 | 1 |
value
|
1 | 12 | 1 | 1 |
name
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
<input type="hidden">
|
HTML Standard #hidden-state-(type=hidden) |