Home > JavaScript > Dynamically Adding and Removing Text Boxes using JavaScript

Dynamically Adding and Removing Text Boxes using JavaScript

After many days I was just trying to work with PHP, MySQL and JavaScript yesterday. In one of the page I wanted to add/remove text boxes on demand (or say dynamically) so after much thought and search i found the solution in using JavaScript. Here is the sample of code which I used for my purpose and may also be beneficial for you web developers who wanted to add or remove text boxes dynamically.

It simply uses two functions:
addElement(): It first gets the element id where we want to include a new element followed by creation of new element and appending it inside the element.
removeElement(): It removes the last added new element.

<html>
<head>
<title>Adding and Removing Text Boxes Dynamically</title>
<script type=”text/javascript”>
var intTextBox=0;

//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement()
{
intTextBox = intTextBox + 1;
var contentID = document.getElementById(‘content’);
var newTBDiv = document.createElement(‘div’);
newTBDiv.setAttribute(‘id’,’strText’+intTextBox);
newTBDiv.innerHTML = “Text “+intTextBox+”: <input type=’text’ id=’” + intTextBox + “‘ name=’” + intTextBox + “‘/>”;
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement()
{
if(intTextBox != 0)
{
var contentID = document.getElementById(‘content’);
contentID.removeChild(document.getElementById(’strText’+intTextBox));
intTextBox = intTextBox-1;
}
}
</script>
</head>
<body>
<p>Demo of Adding and Removing Text Box Dynamically using JavaScript</p>
<p><a href=”javascript:addElement();” >Add</a> <a href=”javascript:removeElement();” >Remove</a></p>
<div id=”content”></div>
</body>
</html>

Just drop me a comment if you have any suggestion, comments or query.

Categories: JavaScript Tags: ,
  1. danny
    July 14, 2008 at 5:09 pm | #1

    why is this code not running at my computer?

    • September 3, 2009 at 2:58 am | #2

      This is simple HTML code with JavScript, I hope you are copying it correctly.

  2. July 17, 2008 at 12:16 pm | #3

    this works fine… but how to read the values of dynamically created textboxes in PHP???

    • September 3, 2009 at 3:02 am | #4

      there are possibly many ways I can suggest two simple ways as:
      1. Treat the textboxes as array and handle it in PHP.
      2. As in above code we are sending name as intTextBox so in PHP page you can loop from 1 to value of intTextBox and gets its value in PHP.
      I hope it will help.

  3. August 3, 2008 at 6:00 am | #5

    Brilliant!

  4. Naresh KUmar
    August 4, 2008 at 10:27 am | #6

    Super! it helped me..Thank you deep!!

  5. Naresh KUmar
    August 4, 2008 at 11:50 am | #7

    Hi Danny! you might copied and pasted the above code as it is I think.. change quotes properly..here the quotes in the above program are looks different..if you observe carefully you can find them..

    Change all quotes in proper way then surely you will get result.

  6. Suresh
    August 25, 2008 at 11:15 am | #8

    It’s working but i can’t read a values enter that dynamicaly generated textboxes. how can i do that? any one help me.

  7. Emman
    January 28, 2009 at 5:42 pm | #9

    Very simple. But works great! Thanks man!

  8. Kev
    February 3, 2009 at 1:26 am | #10

    No IE support, right?

    • September 3, 2009 at 3:05 am | #11

      I tested it in IE6+, FireFox and Opera and its working fine.

  9. Asgar
    June 16, 2009 at 7:17 pm | #12

    Nice, it works fine.
    thanks!!

  10. July 3, 2009 at 1:59 pm | #13

    Does not work for me. What can i do i added the code as it is.

    • September 3, 2009 at 3:07 am | #14

      Hi Utsav,
      This is simple HTML code with JavScript, I hope you are copying it correctly.
      Also check above Naresh Kumar’s suggestion(comment).

  11. sunil deo
    July 9, 2009 at 3:18 pm | #15

    very good i m very thank full to you.

  12. sumit
    July 16, 2009 at 10:31 am | #16

    hey its fine, but how to get save all text box values..please let me know.

  13. Suresh
    July 27, 2009 at 3:02 pm | #17

    Ya this is fine, but how to read the values of dynamically created textboxes in PHP??? . please let me know is possible to read the values in PHP?……..

  14. sweety
    July 30, 2009 at 3:58 pm | #18

    its fine! but how to get save all text box values…

    • September 3, 2009 at 3:10 am | #19

      @sumit, @Suresh, @sweety,
      there are possibly many ways, I can suggest two simple ways:
      1. Treat the textboxes as array and handle it in PHP.
      2. As in above code we are sending name as intTextBox so in PHP page you can loop from 1 to value of intTextBox and gets its value in PHP.
      I hope it will help.

  15. August 17, 2009 at 1:49 pm | #20

    Nice code, thanks

  16. zahid
    September 2, 2009 at 9:35 am | #21

    I have a queestion.

    using this code

    how to validate this code

    like I am making a textbox array.

    If i remove first then validate funciton did not work due to total count issue

    any help

    • September 3, 2009 at 2:56 am | #22

      @zahid: As this code is adding and removing new textbox at the end so total count (intTextBox) can be increased or decreased based on that.

  1. No trackbacks yet.