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

Recommended Answers

All 5 Replies

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

Thank u very much :).........

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

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

thanks....

Be a part of the DaniWeb community

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