How to Create Input Field Dynamically in JavaScript

How to Create Input Field Dynamically in JavaScript

Introduction

In this tutorial we will create a How to Create Input Field Dynamically in JavaScript. This tutorial purpose is to teach how to create input field dynamically. This will cover all the basic function that will show the adding of new input field. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any form that needed more information.. I will give my best to provide you the easiest way of creating this program Dynamic input field. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display form inputs and the button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Create Input Field Dynamically in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-7">
  17. <form method="POST">
  18. <h2>My Form</h2>
  19. <div id="forms" class="form-inline">
  20. <input type="text" class="form-control" style="width:80%;" placeholder="Please enter here" name="form[]">
  21. </div>
  22. <br />
  23. <button type="button" class="btn btn-primary" onclick="addField();">Add Field</button>
  24. </form>
  25. </div>
  26. </div>
  27. <script src="script.js"></script>
  28. </body>
  29. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically add new input field when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var fieldId = 0;
  2.  
  3. function addElement(parentId, elementTag, elementId, html){
  4. var id = document.getElementById(parentId);
  5. var newElement = document.createElement(elementTag);
  6. newElement.setAttribute('id', elementId);
  7. newElement.innerHTML = html;
  8. id.appendChild(newElement);
  9.  
  10. }
  11.  
  12. function removeField(elementId){
  13. var fieldId = "field-"+elementId;
  14. var element = document.getElementById(fieldId);
  15. element.parentNode.removeChild(element);
  16. }
  17.  
  18. function addField(){
  19. fieldId++;
  20. var html = '<br /><input type="text" name="form[]" class="form-control" placeholder="Please enter here" style="width:80%;">' + '<button onclick="removeField('+fieldId+');"><span class="glyphicon glyphicon-minus"></span></button><br />';
  21. addElement('forms', 'div', 'field-'+ fieldId, html);
  22. }

In the first line of code we create a variable called fieldID, this act as the id of the input we create. We will then create a method called addElement(), this method will be the one who will create a new input field dynamically. This include all the creating of new element and append it into the targeted parent. Next we create the remove input method called removeField(), this function purpose is to delete the target field when you clicked the button.

Output:

The How to Create Input Field Dynamically in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Input Field Dynamically in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment