Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Add And Remove Textbox Using JavaScript

Thursday, Sep 28, 2017

Tags:- JavaScript CSS HTML


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


See Demo
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.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.