Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically adding and deleting input boxes

Status
Not open for further replies.

shifter480

Technical User
Apr 28, 2009
12
GB
Hi,

I currently have some JS that allows me to dynamically add input boxes. Once submitted each dynamically added box value is added to an array.

I want to add a button that will allow me to delete the last dynamically added box.

Here is my attempt

<script language="javascript" type="text/javascript" defer>

// This function will dynamically add level fields to the form.
var counter = 1;
var limit = 10;
function addInput(divName)
{
if (counter == limit)
{
alert("You have reached the limit of adding " + counter + " inputs");
}
else
{
counter++;
var newdiv = document.createElement('div' + (counter));
newdiv.innerHTML = "<label>Level"+ (counter)+ ": *</label><input type='text' name='levels[]'>";
document.getElementById(divName).appendChild(newdiv);
}
}
function remove(id)
{
return (elem=document.getElementById(id)).parentNode.removeChild(elem);
}
function removeInput(divName)
{
if (counter > 1)
{
remove('div' + (counter));
--counter;
}
}
</script>


The HTML buttons:

<hr/>
<div id="dynamicRemove">
<input type="button" value="Remove level" onClick="removeInput('dynamicRemove');">
</div>
<div id="dynamicInput">
<label>Level1: *</label> <input type="text" name="levels[]">
<input type="button" value="Add level" onClick="addInput('dynamicInput');">
</div>
<hr/>

The adding works but the remove doesn't. Any ideas would be much appreciated.

Thanks
 
Apologies -
Line: var newdiv = document.createElement('div' + (counter));

should be
var newdiv = document.createElement('div');
 
Hi

This works in Gecko, Presto and WebKit. May fail in Trident, not tested.
Code:
[b]function[/b] [COLOR=darkgoldenrod]removeInput[/color][teal]([/teal]divName[teal])[/teal]
[teal]{[/teal]
     [b]if[/b] [teal]([/teal]counter [teal]>[/teal] [purple]1[/purple][teal])[/teal]
     [teal]{[/teal]
          document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'dynamicInput'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]removeChild[/color][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'dynamicInput'[/i][/green][teal]).[/teal]lastChild[teal])[/teal]
          [teal]--[/teal]counter[teal];[/teal]
     [teal]}[/teal]
[teal]}[/teal]
I kept the change simple, feel free to optimize it.

Note that testing the counter's value will not stop people adding more that 10 [tt]input[/tt]s. So hopefully you are checking that server-side too.


Feherke.
 
Ah yes that works! Thank you very much.

The limit of 10 was just me messing around. Not checked server side as it is not important but a good point regardless!

Much appreciated.
Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top