JQuery is the library that makes the use of JavaScript easy. Inserting the <input type=’hidden’> can also be done using jQuery. The append() and appendTo() inbuilt function can be used to add the hidden form element but they are not only limited to the <input type=’hidden’> but we can also add other html elements.
Note: They both perform pretty much the same. The major difference is only about the syntax and that is explained below.
Using appendTo() method: In the appendTo() method, the content comes before the method like $(content).appendTo(selector).
Below examples illustrates the appendTo() method to create hidden form element:
Example 1: If only one attribute is to be added, then it can be given by passing two arguments in attr() method. First argument is the name of attribute, and the second argument is the value of the attribute.
<!DOCTYPE html> <html> <head> <title>GeeksforGeeks </title> integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"> </script> </head> <body> <script> $(document).ready(function() { $("<input>").attr("type", "hidden").appendTo("form"); }) </script> <form> Name: <input type="text"> <br> Hidden: </form> </body> </html> |
Output: The output can be seen in the browser using Inspect Element feature (e.g. ctrl + shift + i in Google Chrome).

Example 2: Multiple attributes can also be given by passing them as an object of attributes in the attr() method.
<!DOCTYPE html> <html> <head> <title>GeeksforGeeks</title> integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"> </script> </head> <body> <script> $(document).ready(function() { $("<input>").attr({ name: "hiddenField", id: "hiddenId", type: "hidden", value: 10 }).appendTo("form"); }) </script> <form> Name: <input type="text"> <br> Hidden: </form> </body> </html> |
Output: The output can be seen in the browser using Inspect Element feature (e.g. ctrl + shift + i in Google Chrome).

Using append() method: In the append() method, the content comes after the method like $(selector).append(content) .
Below example illustrates the append() method to create hidden form element:
Example:
<!DOCTYPE html> <html> <head> <title>GeeksforGeeks</title> integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script> </head> <body> <script> $(document).ready(function() { $("form").append("<input type='hidden' name='hidField' value='111'>") }) </script> <form> Name: <input type="text"> <br> Hidden: </form> </body> </html> |
Output: The output can be seen in the browser using Inspect Element feature (e.g. ctrl + shift + i in Google Chrome).

Recommended Posts:
- Difference between 'hidden' and 'aria-hidden' attributes in HTML
- Google AMP fly-in animation
- How to check if an element is hidden in jQuery?
- Get the height of the hidden element in jQuery
- How to get the width of hidden element in jQuery ?
- How to Display/Hide functions using aria-hidden attribute in jQuery ?
- How to select all visible or hidden elements in a HTML page using jQuery ?
- How to create Hidden Header using Google AMP amp-accordion?
- jQuery | :hidden Selector
- How to detect value change on hidden input field in JQuery ?
- HTML | DOM Input Hidden form Property
- How to display hidden element when a user starts typing using JavaScript ?
- Create a Form Dynamically using Dform and jQuery
- How to enable/disable all input controls inside a form element using jQuery ?
- HTML | DOM Input Hidden value Property
- HTML | DOM Input Hidden type Property
- Program to find the Hidden Number
- HTML | hidden Attribute
- HTML | DOM Input Hidden Object
- HTML | DOM Input Hidden name Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

