How to Dynamically Remove HTML Input Text Field in JavaScript

How to Dynamically Remove HTML Input Text Field in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Remove HTML Input Text Field in JavaScript. This tutorial purpose is to teach you how to remove textbox dynamically. This will cover all the important functionality that allow you to remove HTML input text field in dynamic way. 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 system or application if you want to remove any input field. I will give my best to provide you the easiest way of creating this program Remove Input Text Field. So let's do the coding.

Before we get started:

Here 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 the html form and the buttons. 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 Dynamically Remove HTML Input Text Field</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <h2>Input Text Fields</h2>
  18. <br />
  19. <form>
  20.  
  21. <div class="form-inline" id="input-1">
  22. <input type="text" class="form-control" placeholder="Enter firstname" name="form[]"><button type="button" class="btn btn-danger btn-sm" onclick="removeInputField(1);">Remove</button>
  23. <br /><br />
  24. </div>
  25. <div class="form-inline" id="input-2">
  26. <input type="text" class="form-control" placeholder="Enter lastname" name="form[]"><button type="button" class="btn btn-danger btn-sm" onclick="removeInputField(2);">Remove</button>
  27. <br /><br />
  28. </div>
  29. <div class="form-inline" id="input-3">
  30. <input type="text" class="form-control" placeholder="Enter address" name="form[]"><button type="button" class="btn btn-danger btn-sm" onclick="removeInputField(3);">Remove</button>
  31. <br /><br />
  32. </div>
  33. <div class="form-inline" id="input-4">
  34. <input type="text" class="form-control" placeholder="Enter occupation" name="form[]"><button type="button" class="btn btn-danger btn-sm" onclick="removeInputField(4);">Remove</button>
  35. <br /><br />
  36. </div>
  37. <div class="form-inline" id="input-5">
  38. <input type="text" class="form-control" placeholder="Enter civil status" name="form[]"><button type="button" class="btn btn-danger btn-sm" onclick="removeInputField(5);">Remove</button>
  39. <br /><br />
  40. </div>
  41. </form>
  42. </div>
  43. </div>
  44. <script src="script.js"></script>
  45. </body>
  46. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove input text 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 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. }
  12.  

This code will only create one method to remove HTML input field called removeInputField(). This function will allow the user to remove the Input Text field by clicking the button in the row. The code is consist of several variable bindings and setting the id for the input. To remove the input text field we use the special function called parentNode.removeChild() and passing the target input text field into it.

Output:

The How to Dynamically Remove HTML Input Text Field 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 Dynamically Remove HTML Input Text Field 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