Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create textfield by using document.createElement().
Posted
Updated 25-Dec-11 19:22pm
v2

1 solution

Look this simple example. and try

<html>
	<head>
		<title>test</title>
		<script type="text/javascript">
			var counter = 0;
			function addNew() 
// Get the main Div in which all the other divs will be added
             var mainContainer =document.getElementById('mainContainer');
				// Create a new div for holding text and button input elements
				var newDiv = class="highlight">document.createElement('input');
				newText.type = "input"; 
				newText.value = counter;
				// Create a new button input
				var newDelButton = ^__strong class="highlight">document.createElement('input');
				newDelButton.type = "button";
				newDelButton.value = "Delete";
				
				// Append new text input to the newDiv
				newDiv.appendChild(newText);
				// Append new button input to the newDiv
				newDiv.appendChild(newDelButton);
				// Append newDiv input to the mainContainer div
				mainContainer.appendChild(newDiv);
				counter++;
				
				// Add a handler to button for deleting the newDiv from the mainContainer
				newDelButton.onclick = function() {
						mainContainer.removeChild(newDiv);
				}
			}
		</script>
	</head>
	
	<body >
		<div id="mainContainer">
			<div><input type="text" ><input type="button" value="Add" onClick="addNew()"></div>
		</div>
	</body>
</html>
 
Share this answer
 
Comments
DeepthiTanguturi 26-Dec-11 1:28am    
I got the error at highlight in javascript code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900