I want to add a text field inside a div(div with id "my_div") tag dynamically(By clicking "Add" button).
The code below shown is adding the new textfield after the submit button not inside the "my_div" div .

<script language="javascript">

function add()

		var divTag = document.createElement("div");
       	divTag.id = "div";
        //divTag.style.margin = "0px auto";
        divTag.className ="dynamicDiv";
        divTag.innerHTML = "<input type='text' name='mytext[]' value='mytext'>";

<form name="f"  id="f" action="homep.php" method="get">

<input type="button" value="Add" onClick="add()">
<div id="my_div" name="my_div">

  <input type="submit" name="Submit" value="Submit">


I had given as following,
for adding textfield inside div.But it showing errors..

Any body knows this solution please help me...


DO no create new div, rather use existing

var divTag = document.getElementById("my_div");
//       	divTag.id = "div";// do change id of your div
//keep rest code as it is


This will allow you to add text fields progressively.

function add()
  var mydiv = document.getElementById("my_div");
  var div = document.createElement("div");
  div.className ="dynamicDiv";
  div.innerHTML = "<input type='text' name='mytext[]' value='mytext' />";


Thank you ............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.