...

View Full Version : adding fields to form onclick



Shecky
01-24-2005, 10:21 AM
I'm trying to make a form where, when a button is clicked, it adds another field for data entry. I need this to work with IE and Firefox. As it seems innerHTML works with firefox, my first instinct was to try that, however it reacts a bit differently in firefox (it clears previous fields when a new field is added.)



<html>
<head>
<script>
function addrow(){
form1.innerHTML += "<input type='text'><br>";
}
</script>
</head>

<body>
<form id="form1">
<input type='text'><br>
</form>
<button onclick="addrow()">Add Row</button>
</body>
</html>


Anyhoo... it doesn't have to be innerHTML, either... just has to work with IE and FF. What is a good way to do this?

Willy Duitt
01-24-2005, 11:35 AM
Something like this should work...



<script type="text/javascript">
function addrow(insertHere){
var div = document.createElement ('DIV');
var input = document.createElement ('INPUT');
input.type = 'text';

div.appendChild(input);
insertHere.parentNode.insertBefore(div,insertHere);
}
</script>
</head>

<body>
<form id="form1" style="border:1px solid black">
<input type='text'><br>
<button onclick="addrow(this)">Add Row</button>
</form>


.....Willy

BTW: What's your button doing outside of the form tags??

Shecky
01-24-2005, 11:14 PM
your code (above) (http://www.technosquid.com/externaljunk/mffsux.htm)
doesn't seem to work in firefox.

hemebond
01-24-2005, 11:47 PM
Yes it does. The form is being submitted and so the page is reloading.

Willy Duitt
01-24-2005, 11:50 PM
Thats because your button is acting like a submit and trying to submit the form... I merely offered a script using the little information you provided as an example... It was not and is not my intention to create an entire form for you, particularly since I am not privy to all of your requirements... Change your <button... to <input type="button" and it will work as intended...

Did you note my original question of why that button was outside of your form tag... And what about a name or id??

I'm out of here...
Good Luck;

.....Willy

Shecky
01-24-2005, 11:55 PM
thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum