HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

HTML Dynamic Form

How to Use JavaScript to Construct a form Dynamically

Using the Document Object Model (DOM) functions createElement(), setAttribute(), and appendChild(), one may create a completely dynamic HTML form. With the aid of these DOM techniques, we may create a dynamic HTML form. To create this dynamic form, we will create each element within the script tag, modify its attribute to add functionality, and then attach it to the element's parent after our element is ready to use. Therefore, any element attached to the parent form tag is a child element.

Syntax

The syntax applied to this task.

  1. The createElement() function can create any HTML element. Examples include div, button, p, label, and so forth.
     document.createElement(elements);
  • To put an attribute in an element, use the setAttribute() function (document.createElement(elements);). A key value pair containing the value is supplied into the setAttribute() function. For instance, ("placeholder," "Name,"), ("type," "submit,"), ("value," "submit,") and so on.
element.setAttribute(“Key”,“Value”);
  • The element we constructed using the createElement() function is inserted into any body tag using the appendChild() method. The appendChild() function receives the direct element's element reference as a parameter.
parentElement.appendChild(element);

Step 1: Using your editor, create a basic HTML boilerplate code. Additionally, make a button and a form tag so that our dynamic form can be loaded inside of them.

Step 2: Within the script tag, create a javascript arrow function.

Step 3: Use the document to obtain the form into a variable.getElementById() since the id name defines the form tag.

Step 4: At this point, begin creating your dynamic shape. Use the document's createElement() function to create a new element.

var userName = document.createElement("input");

Step 5: Use the setAttribute() function to set the attribute in the element that was created above.

userName.setAttribute("placeholder", "Name");

Step 6: Append the previously formed element with the id name "dform" to the parent element "form" using the appendChild() function. 

dform.appendChild(userName);

Step 7: Completing all mandatory areas on the form requires reiterating the actions from actions 4 through 6.

Step 8: Use createElement() to create a new element div. Additionally, create two buttons, submit and reset, respectively, and add them to the element.

Step 9: Select the "Generate Form" button to initiate the "gForm()" function, which will produce a dynamic form.

Example

In the example provided, we used Javascript to create a form. As a result, no element inside the form tag is constructed with the same level of staticity as those inside the body tag. The form has fields where students can register. These fields—name, email, address, date of birth, and phone number—are all dynamically generated using script tags.

<html>

<head>

   <title>Create form dynamically with js</title>

</head>

   <body style="height: 70vh; width: 40%;display:flex;flex-direction: column;place-content: center; margin: auto;">

   <p style="margin: 0 auto;">Example of Student registration form</p>

   <button style="margin: 0.3rem auto;width: 5rem;" onclick="Form()"> Generate Form </button>

   <form id="dynamicForm"> </form>

   <script>

      Form = () => {

         var dform = document.getElementById("dynamicForm");

         dform.setAttribute("style", "display:flex;flex-direction:column")

         var userName = document.createElement("input");

         userName.setAttribute("placeholder", "Name");

         dform.appendChild(userName);

         var userEmail = document.createElement("input");

         userEmail.setAttribute("placeholder", "Email");

         userEmail.setAttribute("type", "email");

         dform.appendChild(userEmail);

         var userAdd = document.createElement("input");

         userAdd.setAttribute("placeholder", "Address");

         dform.appendChild(userAdd);

         var userDob = document.createElement("input");

         userDob.setAttribute("placeholder", "D.O.B");

         userDob.setAttribute("type", "date");

         dform.appendChild(userDob);

         var userPhn = document.createElement("input");

         userPhn.setAttribute("placeholder", "Phone number");

         dform.appendChild(userPhn);

         var sBtn = document.createElement("input");

         sBtn.setAttribute("value", "submit");

         sBtn.setAttribute("type", "submit");

         var rBtn = document.createElement("input");

         rBtn.setAttribute("value", "reset");

         rBtn.setAttribute("type", "reset");

         var btns = document.createElement("div");

         btns.setAttribute("style","margin:0.3rem auto")

         dform.appendChild(btns);

         btns.appendChild(sBtn);

         btns.appendChild(rBtn);

      }

   </script>

</body>

</html>

Output

HTML Dynamic Form

Explanation for the above code

The outcome of the example above, which shows the button to produce a student registration form, is seen in the image below. If you do not click the produce form button, the page that appears is basic and looks like the one below.

The script tag's arrow function is activated when the "Generate Form" button above is pressed. The result is a dynamically produced form with all the fields needed for student registration.

Conclusion

By completing this assignment, we may construct any dynamic element on our website. Because the server does not have to react to a large, bulky piece of code at the beginning of the page loading process, the dynamic content on the page speeds up page loading times. The page is also more engaging because of its dynamic shape.