JavaScript - Remove Input Field Dynamically

In this tutorial we will create a Remove Input Field Dynamically using JavaScript. This code will dynamically remove input field when the user click the remove button. The code use onclick() to call a function that remove an input field by applying an array index position as the parameter in the removeInputField(). You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScript - Remove Input Field Dynamically</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <form >
  19.                                 <h3>Input Fields</h3>
  20.                                 <div class="form-inline" id="input-1">
  21.                                         <input type="text" class="form-control" placeholder="Enter some text" name="form[]"><button class="btn btn-danger" onclick="removeInputField(1);">Remove</button>
  22.                                         <br /><br />
  23.                                 </div>
  24.                                 <div class="form-inline" id="input-2">
  25.                                         <input type="text" class="form-control" placeholder="Enter some text" name="form[]"><button class="btn btn-danger" onclick="removeInputField(2);">Remove</button>
  26.                                         <br /><br />
  27.                                 </div>
  28.                                 <div class="form-inline" id="input-3">
  29.                                         <input type="text" class="form-control" placeholder="Enter some text" name="form[]"><button class="btn btn-danger" onclick="removeInputField(3);">Remove</button>
  30.                                         <br /><br />
  31.                                 </div>
  32.                                 <div class="form-inline" id="input-4">
  33.                                         <input type="text" class="form-control" placeholder="Enter some text" name="form[]"><button class="btn btn-danger" onclick="removeInputField(4);">Remove</button>
  34.                                         <br /><br />
  35.                                 </div>
  36.                                 <div class="form-inline" id="input-5">
  37.                                         <input type="text" class="form-control" placeholder="Enter some text" name="form[]"><button class="btn btn-danger" onclick="removeInputField(5);">Remove</button>
  38.                                         <br /><br />
  39.                                 </div>
  40.                         </form>
  41.                 </div>
  42.         </div>
  43. <script src="js/script.js"></script>   
  44. </body>
  45. </html>

Creating the Script

This code contains the script of the application. This code will remove an input field when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var counter=5;
  2.  
  3. function removeInputField(id){
  4.         var inputId = "input-"+id;
  5.         var input = document.getElementById(inputId);
  6.         input.parentNode.removeChild(input);
  7.         counter--;
  8.         if(counter==0){
  9.                 window.location="index.html";
  10.         }
  11. }
There you have it we successfully created a Remove Input Field Dynamically using 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!

Add new comment