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!

Problem with adapting code

Status
Not open for further replies.

ThorWiumGiversen

Programmer
Feb 17, 2012
1
DK
Hi i am messing around with some code from and i´ve run in to a problem. I am trying to dynamically create <textareas> and for some reason i cannot remove the <switch>. Here is what i have in my script

<script>
var counterTextArea = 1;
function addAllInputs(divName, inputType){
var newdiv = document.createElement('div');
switch(inputType) {
case 'textarea':
newdiv.innerHTML = "Entry " + (counterTextArea) + " <br><textarea name='myTextAreas[]'>type here...</textarea>";
counterTextArea++;
}
document.getElementById(divName).appendChild(newdiv);
}
</script>



and here is what i have in html

<form name="myForm" method="POST">
<div id="dynamicInputs"></div>
<input type="button" value="Add new Chapter" onClick="addAllInputs('dynamicInputs', 'textarea');">
</form>


I hope that someone can help me.

Regards Thor Giversen
 
What do you mean "cannot remove the <switch>"? Just delete the line and it is removed.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Hi

You mean [tt]textarea[/tt] is the only [tt]form[/tt] element you will ever want to add ?
JavaScript:
[b]var[/b] counterTextArea [teal]=[/teal] [purple]1[/purple][teal];[/teal]

[b]function[/b] [COLOR=darkgoldenrod]addAllInputs[/color][teal]([/teal]divName[teal])[/teal]
[teal]{[/teal]
    [b]var[/b] newdiv [teal]=[/teal] document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'div'[/i][/green][teal]);[/teal]

    newdiv[teal].[/teal]innerHTML [teal]=[/teal] [green][i]"Entry "[/i][/green] [teal]+[/teal] [teal]([/teal]counterTextArea[teal])[/teal] [teal]+[/teal] [green][i]" <br><textarea  name='myTextAreas[]'>type here...</textarea>"[/i][/green][teal];[/teal]
    counterTextArea[teal]++;[/teal]

    document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]divName[teal]).[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]newdiv[teal]);[/teal]
[teal]}[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top