View Full Version : Dynamically add textbox for user entry and saving data entered

05-05-2009, 08:14 PM
I have an ASP page that displays, along with other data, a list of items that refer to a specific element in a database. Instead of adding an Add button, I would like to display a text box below the last item in the list to have the user input any new data. Once they enter the data and hit Enter, the data they enter should then be added to the list and another text box added beneath this item (just as forms and tables in Access do).
However, I'm not sure how to implement this functionality. Can someone please help me out?


Philip M
05-05-2009, 08:54 PM
Try this:-

<input type="hidden" value="0" id="theValue" />
<div id="myDiv"> </div>

<script type = "text/javascript">

function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.innerHTML = "Item " + num + " " + "<input type='text' size = '60' name='poll_option+num' onblur = 'capture(this)'/>"

function capture(which) {
x = which.value;
alert (x); // add data to an array or whatever


<input type = "button" name = "but1" id = "but1" value = "Add Some More Data" onclick = "addElement()">

Or if you want to dispense with the button:-

<body onload = "addElement()">

newdiv.innerHTML = "Item " + num + " " + "<input type='text' size = '60' name='poll_option+num' onblur = 'capture(this);addElement()'/>"

"He has been in the final of the tournament before, and people often go on from there to win it ...." Snooker commentator.