Avatar of stevong
stevong asked on

Javascript: Dynamically create new text fields

Hi everyone,

I wish to dynamically create new textbox/controls when user clicks on a hyperlink. Attached is the javascript that I've used.

But on Non-IE browsers, the values that was key-in the previous textbox will not be retained.

Any ways to over come it?

Please advise.

Thanks
<script language="javascript">
var i = 1;
function addnew()
{
	i++;
	var s = "<div>Contents:<br /><textarea name=\"contents" + i +  "\"></textarea></div>";
	document.getElementById("subbox").innerHTML = document.getElementById("subbox").innerHTML + s;
}
</script>

Open in new window

Scripting Languages

Avatar of undefined
Last Comment
stevong

8/22/2022 - Mon
MikeRCWatts

Which browsers are you having trouble with?

This works OK for me on IE, Firefox 2, Opera, as below.

?Mike
<html>
<head>
 
<script language="javascript">
var i = 1;
function addnew()
{
	i++;
	var s = "<div>Contents:<br /><textarea name=\"contents" + i +  "\"></textarea></div>";
	document.getElementById("subbox").innerHTML = document.getElementById("subbox").innerHTML + s;
}
</script>
 
</head>
<body>
 
<input type=button value='Click to add another' onclick=addnew()>
<div id=subbox>
Div subbox
</div>
</body>
</html>

Open in new window

ASKER
stevong

Thanks for response.

Yes. the code generates another control dynamically.

eg:

you have 1 textbox. u key in "hello world"

then u click "Add"

Another text box appears. u key in "hello world 2". But the previous text box - the "hello world " is gone.

U click add again, the 3rd text box appears, but "hello world 2" and "hello world" is gone. the previous textbox remains empty.


Regards

ASKER CERTIFIED SOLUTION
MikeRCWatts

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
stevong

Thanks. It works :)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23