Categories
JavaScript Answers

How to pass a JavaScript variable value into input type hidden value?

Spread the love

Sometimes, we want to pass a JavaScript variable value into input type hidden value.

In this article, we’ll look at how to pass a JavaScript variable value into input type hidden value.

How to pass a JavaScript variable value into input type hidden value?

To pass a JavaScript variable value into input type hidden value, we can set its value property.

For instance, we write

<input type="hidden" id="myField" value="" />

to add a hidden input.

Then we write

document.getElementById("myField").value = product(2, 3);

to select the input with getElementById.

Then we set its value property to the return value of the product function to set the value attribute of the input.

Conclusion

To pass a JavaScript variable value into input type hidden value, we can set its value property.

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 *