All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Add And Remove Textbox Using JavaScript

Last Updated : Jul 1, 2023

IN - JavaScript CSS HTML | Written & Updated By - Dikshita

In this tutorial we will create show how to add and remove textbox in a form dynamically using javascript. You may also like Add,Edit And Delete Rows From Table Dynamically Using JavaScript

Add And Remove Textbox Using JavaScript

To Add And Remove Textbox it takes only one step:-

  1. Make a HTML file and define markup and script to add and remove textbox

Step 1. Make a HTML file and define markup and script to add and remove textbox

We make a HTML file and save it with a name add_remove.html

<html>
<head>
<script>
function add_field()
{
  var total_text=document.getElementsByClassName("input_text");
  total_text=total_text.length+1;
  document.getElementById("field_div").innerHTML=document.getElementById("field_div").innerHTML+
  "<p id='input_text"+total_text+"_wrapper'><input type='text' class='input_text' id='input_text"+total_text+"' placeholder='Enter Text'><input type='button' value='Remove' onclick=remove_field('input_text"+total_text+"');></p>";
}
function remove_field(id)
{
  document.getElementById(id+"_wrapper").innerHTML="";
}
</script>
<style>
body
{
  margin:0 auto;
  padding:0;
  text-align:center;
  height:2000px;
  background-color:silver;
}
#wrapper
{
  width:995px;
  padding:0px;
  margin:0px auto;
  font-family:helvetica;
  text-align:center;
}
input[type="text"]
{
  width:200px;
  height:35px;
  margin-right:2px;
  border-radius:3px;
  border:1px solid green;
  padding:5px;
}
input[type="button"]
{
  background:none;
  color:white;
  border:none;
  width:110px;
  height:35px;
  border-radius:3px;
  background-color:green;
  font-size:16px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="field_div">
<input type="button" value="Add TextBox" onclick="add_field();">
</div>
</div>
</body>
</html>

In this step we create a add textbox button to create text box.We made two functions one is used to create and insert textbox and other is used to remove the particular textbox when there respective button is clicked.

You may also like Check And Uncheck Checkboxes Using JavaScript

Thats all, this is how to Add And Remove Textbox Using JavaScript.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on add input text and remove input text JavaScript helps you and the steps and method mentioned above are easy to follow and implement.