How to Create a Form Dynamically with the JavaScript? Last Updated : 09 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The task is to create an HTML form dynamically with the help of JavaScript. There are two approaches that are discussed below.Approach 1: Use document.createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the <form> element by appendChild() method. Finally append the <form> element to the <body> element of the document. This example creates a Registration form.Example: html <!DOCTYPE html> <html> <head> <title> Create a Form Dynamically with the JavaScript </title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksforGeeks </h1> <p> Click on the button to create a form dynamically </p> <button onClick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"></p> <script> var down = document.getElementById("GFG_DOWN"); // Create a break line element var br = document.createElement("br"); function GFG_Fun() { // Create a form dynamically var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "submit.php"); // Create an input element for Full Name var FN = document.createElement("input"); FN.setAttribute("type", "text"); FN.setAttribute("name", "FullName"); FN.setAttribute("placeholder", "Full Name"); // Create an input element for date of birth var DOB = document.createElement("input"); DOB.setAttribute("type", "text"); DOB.setAttribute("name", "dob"); DOB.setAttribute("placeholder", "DOB"); // Create an input element for emailID var EID = document.createElement("input"); EID.setAttribute("type", "text"); EID.setAttribute("name", "emailID"); EID.setAttribute("placeholder", "E-Mail ID"); // Create an input element for password var PWD = document.createElement("input"); PWD.setAttribute("type", "password"); PWD.setAttribute("name", "password"); PWD.setAttribute("placeholder", "Password"); // Create an input element for retype-password var RPWD = document.createElement("input"); RPWD.setAttribute("type", "password"); RPWD.setAttribute("name", "reTypePassword"); RPWD.setAttribute("placeholder", "ReEnter Password"); // create a submit button var s = document.createElement("input"); s.setAttribute("type", "submit"); s.setAttribute("value", "Submit"); // Append the full name input to the form form.appendChild(FN); // Inserting a line break form.appendChild(br.cloneNode()); // Append the DOB to the form form.appendChild(DOB); form.appendChild(br.cloneNode()); // Append the emailID to the form form.appendChild(EID); form.appendChild(br.cloneNode()); // Append the Password to the form form.appendChild(PWD); form.appendChild(br.cloneNode()); // Append the ReEnterPassword to the form form.appendChild(RPWD); form.appendChild(br.cloneNode()); // Append the submit button to the form form.appendChild(s); document.getElementsByTagName("body")[0] .appendChild(form); } </script> </body> </html> Output:Approach 2: This approach is somewhat similar to the previous one but using the JQuery method to append the elements. Use document.createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the < form > element by append() method of JQuery. Finally append the <form> element to the <body> element of the document. This example creates a LOGIN form. Example: html <!DOCTYPE html> <html> <head> <title> Create a Form Dynamically with the JavaScript </title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksforGeeks </h1> <p> Click on the button to create a form dynamically </p> <button onClick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"></p> <script> var down = document.getElementById("GFG_DOWN"); function GFG_Fun() { // Create a form dynamically var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "submit.php"); // Create an input element for emailID var ID = document.createElement("input"); ID.setAttribute("type", "text"); ID.setAttribute("name", "emailID"); ID.setAttribute("placeholder", "E-Mail ID"); // Create an input element for password var PWD = document.createElement("input"); PWD.setAttribute("type", "password"); PWD.setAttribute("name", "password"); PWD.setAttribute("placeholder", "Password"); // Create a submit button var s = document.createElement("input"); s.setAttribute("type", "submit"); s.setAttribute("value", "Submit"); // Append the email_ID input to the form form.append(ID); // Append the password to the form form.append(PWD); // Append the button to the form form.append(s); document.getElementsByTagName("body")[0] .appendChild(form); } </script> </body> </html> Output:JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. Comment More infoAdvertise with us Next Article How to Create a Form Dynamically with the JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-form JavaScript-Questions Similar Reads How to create XML Dynamically using JavaScript? XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to create XML Dynamically using JavaScript: Table of Content Usi 2 min read How to dynamically create new elements in JavaScript ? New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method. The examples given below would demonstrate this approach. Example 1: In this example, a newly created element is added as a 4 min read Dynamically Create and Remove Iframe in JavaScript JavaScript allows us to dynamically add and remove elements to the document. We can use event listeners and special JavaScript methods to create and remove HTML elements. This article focuses on creating and removing iframe elements dynamically using JavaScript.Let's first create a basic structure o 3 min read How to create the dynamic forms in AngularJS ? In AngularJS, we need to create dynamic forms to make the user interact. We can create and apply different behaviors to the forms to represent and accept the data from the user. We can create dynamic forms using ng-repeat and ng-show in AngularJS. In this article, we will see how we can create dynam 6 min read How to Create a Form with Multiple Steps using CSS and JavaScript ? A multi-step form divides a long form into smaller, more manageable sections or steps. This makes it easier for users to fill out and makes the form less intimidating. We will explore the approach to building a multi-step form using CSS and JavaScript. ApproachCreate a form container. Inside the for 3 min read Like