How to create hidden form element on the fly using jQuery ?
Last Updated :
31 Oct, 2019
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.
html
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks </title>
<script src="https://code.jquery.com/jquery-3.4.1.js"
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.
html
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks</title>
<script src="https://code.jquery.com/jquery-3.4.1.js"
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:
html
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks</title>
<script src="https://code.jquery.com/jquery-3.4.1.js"
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).
Similar Reads
How to create a hidden input field in form using HTML ? In this article, we will learn how to add a hidden input field into our form using HTML. A hidden control stores the data that is not visible to the user. It is used to send some information to the server which is not edited by the user. Normally, this hidden field usually contains the value which i
2 min read
How to create an HTML element using jQuery ? In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. The jQuery append() method is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html)
2 min read
How to make Form element button using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making Form element button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="styles
1 min read
How to make Mini Form element button using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making Mini Form element button using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="s
1 min read
How to create a Form Popup using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a form popup using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href=âh
2 min read