Create a Form Dynamically using Dform and jQuery - GeeksforGeeks

27 Jul, 2020
If you have ever used Google forms, you might have wondered how does it work. How an end-user can create there form dynamically. If these questions ever came across your mind, then this article can help you.
Prerequisite:
Basic of HTML and CSS
Basic of JQuery
The plugin which we are going to use here is dform. The jQuery.dForm plugin generates HTML markup from JavaScript objects and JSON for HTML forms.
What you can do with the dform plugin?
You can naturally, generate JavaScript-enhanced markup with your own extensions and custom types.
You can use JSON and JavaScript instead of HTML markup.
It’s an easy way to include JQuery UI elements.
Scaffold forms from business objects of your server-side framework.
For More Details you can refer here:  Click Here
How to use this plugin?
Create an empty file, name it index.js to use it in our HTML file.
Click here and copy the whole text, paste it in index.js.
Save index.js.
The plugin is ready to use.
Approach:
We will use type classifiers for adding form input fields.
See the myFunction1 and myFunction2 in code for used classifiers and their attributes. Each and every attribute could be used in the same way.
Types of Classifiers: All the type classifiers are sent in the function in JSON format. Here are some type classifiers:
{“type” : “container”}
{“type” : “div”}
Example: Here is the basic example to show how this can be used.
HTML
<!DOCTYPE html><html>  <body>    <!-- Including jQuery -->    <script src="https://code.jquery.com/jquery-1.12.4.min.js">    </script>          <!-- Including index.js that we         had just created -->    <script type="text/javascript"         src="index.js">    </script>      <script>        $(document).ready(function () {              // Defining  myFunction1             $.fn.myFunction1 = function () {                $("#myform").dform({                    "html": [{                        // Other attributes                        "name": "username",                        "id": "txt-username",                        "caption": "username",                                  // Type Classifier                        "type": "text",                        "placeholder":                         "E.g. user@example.com"                    },                    { "type": "br" }                    ]                });            }            $(".call-btn-text").click(function () {                  // Calling myFunction1 on click                 $.fn.myFunction1();            });        });          $(document).ready(function () {              // Defining  myFunction2             $.fn.myFunction2 = function () {                $("#myform").dform({                    "html": [{                        // Other attributes                        "name": "PhoneNumber",                        "id": "num_phone",                        "caption": "Phone Number",                                  // Type Classifier                        "type": "number",                        "placeholder": "E.g. 0123456789"                    },                    { "type": "br" }                    ]                });            }            $(".call-btn-number").click(function () {                  // Calling myFunction2 on click                $.fn.myFunction2();            });        });    </script>    <form>        <input type="button" class="call-btn-text"             value=" Click me to input text">        <br>        <input type="button" value=            "Click me to input number"             class="call-btn-number">        <br>    </form>    <form id="myform"></form></body>  </html>
Output:
Before Clicking the Button:
When the page loads
After Clicking First Button:
After Clicking First Button
After Clicking Second Button:
After Clicking Second Button
CSS-Misc
HTML-Misc
JavaScript-Misc
jQuery-Misc
CSS
HTML
JavaScript
JQuery
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Create a Responsive Navbar using ReactJS
Design a web page using HTML and CSS
How to set div width to fit content using CSS ?
Making a div vertically scrollable using CSS
How to set fixed width for <td> in a table ?
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
Hide or show elements in HTML using display property
How to Insert Form Data into Database using PHP ?
REST API (Introduction)