- Set Hidden Field Value In JavaScript
- Example of Setting an Input Field Value to Hidden In JavaScript
- Other Articles You’ll Also Like:
- About The Programming Expert
- Hide html hidden field
- Value
- Additional attributes
- name
- Using hidden inputs
- Tracking edited content
- Improving website security
- Validation
- Examples
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- The 6 Best Ways to Hide Text in HTML Code
- How to Hide Text in HTML code
- 1. Hide Text in HTML code with the Global Hidden Attribute
- 2. Hide Text in HTML Code with the style CSS set to display:none
- 3. Hide Text in HTML Code with style CSS set to visibility:hidden
- 4. Hide Text in HTML Code by setting the CSS value to content-visibility:hidden (or auto for improved rendering)
- 5. Hide Text in HTML Code by Setting the Opacity to Zero
Set Hidden Field Value In JavaScript
We can set a hidden field value in a form in JavaScript by changing the type property of the field with the help of the getElementById() method.
document.getElementById("someField").type = "hidden";
The above code would change the property type to hidden.
Let’s take a look at an example below:
Let’s say we have the following HTML form:
As you can see in the code above, we have two simple text input fields.
If we wanted to hide the input field for Full Name, we would use the following JavaScript code:
document.getElementById("fname").type = "hidden";
It can be very useful to hide form fields from the user. Just note that when a form field has the type of hidden, it will not be able to be seen on the webpage by the user. However, the user could still access the information in the hidden field with the use of a browser inspector that most browsers have. So you should make sure not to store any data in the hidden field that you do not want someone to have access to.
Example of Setting an Input Field Value to Hidden In JavaScript
In this simple example, we will just have a standard HTML form. We will provide the HTML code for it and let the user enter a name and submit the form. The form will not have any backend connected to it, so when you submit the form, it should just refresh the page and put the name variable you entered in the URL.
The form you will see will only show a name field, its label, and a submit button. But there is also a hidden email field. We have added JavaScript code to this page to hide the field right away.
Here is that JavaScript code:
document.getElementById("email").type = "hidden";
Here is the form for you to see with the hidden email field:
And here is the Full Code and JavaScript:
Hopefully this article has been useful for you to understand how to set a hidden field value in JavaScript.
Other Articles You’ll Also Like:
- 1. JavaScript Sorted Map – How to Sort a Map by Keys or Values
- 2. Reverse a String in JavaScript
- 3. Using JavaScript to Get Date Format in dd mm yyyy
- 4. Using Javascript to Remove Class from Element
- 5. Check if Character is Uppercase in JavaScript
- 6. Remove All Instances of Value From an Array in JavaScript
- 7. JavaScript Random Boolean – How to Generate Random Boolean Values
- 8. Using JavaScript to Get the Last Day of the Month
- 9. Using JavaScript to Get the Domain From URL
- 10. outerHTML – How to Get and Change the outerHTML Property of an Element
About The Programming Expert
The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.
Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.
At the end of the day, we want to be able to just push a button and let the code do it’s magic.
You can read more about us on our about page.
Hide html hidden field
Note: The input and change events do not apply to this input type. Hidden inputs cannot be focused even using JavaScript (e.g. hiddenInput.focus() ).
Value
The element’s value attribute holds a string that contains the hidden data you want to include when the form is submitted to the server. This specifically can’t be edited or seen by the user via the user interface, although you could edit the value via browser developer tools.
Warning: While the value isn’t displayed to the user in the page’s content, it is visible—and can be edited—using any browser’s developer tools or «View Source» functionality. Do not rely on hidden inputs as a form of security.
Additional attributes
name
This is actually one of the common attributes, but it has a special meaning available for hidden inputs. Normally, the name attribute functions on hidden inputs just like on any other input. However, when the form is submitted, a hidden input whose name is set to _charset_ will automatically be reported with the value set to the character encoding used to submit the form.
Using hidden inputs
As mentioned above, hidden inputs can be used anywhere that you want to include data the user can’t see or edit along with the form when it’s submitted to the server. Let’s look at some examples that illustrate its use.
Tracking edited content
One of the most common uses for hidden inputs is to keep track of what database record needs to be updated when an edit form is submitted. A typical workflow looks like this:
- User decides to edit some content they have control over, such as a blog post, or a product entry. They get started by pressing the edit button.
- The content to be edited is taken from the database and loaded into an HTML form to allow the user to make changes.
- After editing, the user submits the form, and the updated data is sent back to the server to be updated in the database.
The idea here is that during step 2, the ID of the record being updated is kept in a hidden input. When the form is submitted in step 3, the ID is automatically sent back to the server with the record content. The ID lets the site’s server-side component know exactly which record needs to be updated with the submitted data.
You can see a full example of what this might look like in the Examples section below.
Improving website security
Hidden inputs are also used to store and submit security tokens or secrets, for the purposes of improving website security. The basic idea is that if a user is filling in a sensitive form, such as a form on their banking website to transfer some money to another account, the secret they would be provided with would prove that they are who they say they are, and that they are using the correct form to submit the transfer request.
This would stop a malicious user from creating a fake form, pretending to be a bank, and emailing the form to unsuspecting users to trick them into transferring money to the wrong place. This kind of attack is called a Cross Site Request Forgery (CSRF); pretty much any reputable server-side framework uses hidden secrets to prevent such attacks.
Note: Placing the secret in a hidden input doesn’t inherently make it secure. The key’s composition and encoding would do that. The value of the hidden input is that it keeps the secret associated with the data and automatically includes it when the form is sent to the server. You need to use well-designed secrets to actually secure your website.
Validation
Hidden inputs don’t participate in constraint validation; they have no real value to be constrained.
Examples
Let’s look at how we might implement a simple version of the edit form we described earlier (see Tracking edited content), using a hidden input to remember the ID of the record being edited.
The edit form’s HTML might look a bit like this:
form> div> label for="title">Post title:label> input type="text" id="title" name="title" value="My excellent blog post" /> div> div> label for="content">Post content:label> textarea id="content" name="content" cols="60" rows="5"> This is the content of my excellent blog post. I hope you enjoy it! textarea> div> div> button type="submit">Update postbutton> div> input type="hidden" id="postId" name="postId" value="34657" /> form>
Let’s also add some simple CSS:
html font-family: sans-serif; > form width: 500px; > div display: flex; margin-bottom: 10px; > label flex: 2; line-height: 2; text-align: right; padding-right: 20px; > input, textarea flex: 7; font-family: sans-serif; font-size: 1.1rem; padding: 5px; > textarea height: 60px; >
The server would set the value of the hidden input with the ID » postID » to the ID of the post in its database before sending the form to the user’s browser and would use that information when the form is returned to know which database record to update with modified information. No scripting is needed in the content to handle this.
The output looks like this:
Note: You can also find the example on GitHub (see the source code, and also see it running live).
When submitted, the form data sent to the server will look something like this:
Even though the hidden input cannot be seen at all, its data is still submitted.
Technical summary
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Mar 13, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
The 6 Best Ways to Hide Text in HTML Code
Do you need a solution to hide text in your HTML code rather than deleting it? It can be done and easily too. In fact, there is more than one way to do it.
Which method you use to hide HTML text depends on the reason you want it hidden.
Entire paragraphs can be hidden, hidden input fields in HTML forms can be used, or you can style your CSS to hide HTML content within a div tag.
As you read on, you will discover the exact HTML codes to use for different scenarios to hide HTML code at will.
How to Hide Text in HTML code
Using pure HTML, the global hidden attribute hides all HTML code within the tags. With HTML/CSS, you can set the CSS style to display:none, visibility:hidden, content-visibility:hidden, or set the opacity to zero. In HTML forms, setting the input type to hidden will hide the HTML field.
1. Hide Text in HTML code with the Global Hidden Attribute
The global hidden attribute is exactly what the name suggests. The recognized HTML tag hides the HTML content contained within the element.
To use it, append your opening paragraph HTML code with the word hidden (before the closing tag).
2. Hide Text in HTML Code with the style CSS set to display:none
CSS is a preferable way to control the display of HTML elements. It is all to do with styling a HTML document.
One method used to hide HTML content is ‘display:none’. This hides the element in its entirety.
To set any CSS, a selector/identifier is first assigned between the opening and closing style tags by starting with a period (dot), followed by the CSS selector word(s).
It can be anything. For simplicity, since the purpose is to hide HTML content, name it hidden.
The CSS selector is placed between the opening and closing style tags, which are in the head of your document.
To assign the value, place the content that you want to hide within div tags and then declare the class. The div class is the name used as the CSS selector.
3. Hide Text in HTML Code with style CSS set to visibility:hidden
The ‘visibility:hidden’ CSS will hide HTML code, but the element will still be rendered. When the page loads, rather than hiding the block element, the HTML becomes hidden, but the block is still rendered.
What you are left with is an empty block. Whitespace where the HTML content would be shown if it were not disguised behind the visibility hidden code.
Then to hide the text within your HTML document, call the class up with a div tag.
4. Hide Text in HTML Code by setting the CSS value to content-visibility:hidden (or auto for improved rendering)
The content-visibility option is a relatively new CSS addition introduced to improve page rendering time.
It can be set to ‘hidden’, in which case none of the block content will be rendered by browsers.
In comparison, you can also set below-the-fold content to ‘auto’, which will still be rendered by the browser, but only once the content has entered the user’s viewport.
There is a substantial difference between hidden and auto with the content-visiblity tag.
- Hidden works similar to the ‘display:none’ property
- ‘Auto’ delays rendering the HTML content. It does not hide it from the user. It merely delays the rendering of the HTML by user agents until the content is required. That is once the element is within the users’ viewport.
If using the content-visibility tag, set it to hidden to hide the HTML content. Set it to auto if you only want to delay the page rendering time to improve page load speed. (Consider ‘auto’ an alternative to lazy loading ).
5. Hide Text in HTML Code by Setting the Opacity to Zero
This is another way to hide HTML code from the viewport, although the ethics of doing so are murky.
It is essentially the same as the old-school method of setting the text color to be the same as the background color, thus, making the text invisible to the user.
The only difference with the opacity being set to zero is that the text is fully transparent rather than the same color as the background.
All that is needed for this method is a CSS selector.