0

Hi...

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 .

<html>
<head>
<script language="javascript">

function add()
{

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

</script>
</head>
<body>
<form name="f"  id="f" action="homep.php" method="get">

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

</div>
<br>
  <input type="submit" name="Submit" value="Submit">



</form>
</body>
</html>

I had given as following,
document.f.my_div.appendChild(divTag);
for adding textfield inside div.But it showing errors..

Any body knows this solution please help me...

4
Contributors
5
Replies
9
Views
6 Years
Discussion Span
Last Post by manu555
-1

document.createElement("div");

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
.
.
0

Divyakrishnan,

This will allow you to add text fields progressively.

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

Airshow

Edited by Airshow: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.