Categories
JavaScript Answers

How to create a hidden field in JavaScript?

Spread the love

Sometimes, we want to create a hidden field in JavaScript.

In this article, we’ll look at how to create a hidden field in JavaScript.

How to create a hidden field in JavaScript?

To create a hidden field in JavaScript, we call createElement.

For instance, we write

const input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "name");
input.setAttribute("value", "value");

document.body.appendChild(input);

to create an input with createElement.

Then we call setAttribute to set the type, name, and value attribute values of the input.

We set the type attribute to 'hidden' to make it hidden

Next we call appendChild to append the input to the body element as its last child.

Conclusion

To create a hidden field in JavaScript, we call createElement.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *