[Javascript] Retaining values
tedd
tedd at sperling.com
Tue Apr 12 06:39:15 CDT 2011
At 10:59 AM +0200 4/12/11, J. Lester Novros II wrote:
>Tedd,
>
>On 04/11/2011 07:22 PM, tedd wrote:
>>How can I get this to work so that the user can input values and can
>>add additional text fields without losing all the data previously
>>provided?
>
>Change your addInput function to the flwg:
>
> function addInput()
> {
> var newID = document.getElementById('text').childNodes.length + 1;
>
> var ip = document.createElement('input');
> ip.id = 't' + newID;
> ip.type = 'text';
> ip.size = 36;
> ip.name = 'option[]';
> ip.style.display = 'block';
> document.getElementById('text').appendChild(ip);
> ip.focus();
> }
>
>Moral of the story: 'innerHTML == EVIL!'
>
>Like the old 'document.write()' before it, it can't be trusted to
>leave your document alone.
>
>So in future use standard DOM methods [like 'appendChild'] and
>generally you'll be fine [tho' it's a bit more verbose].
>
>l8R lES
J;
Bingo -- that worked like a charm!
I have no reservations about code being a bit more verbose if it: a)
get's the job done; b) and is easy to understand. And your code
accomplishes both.
Many thanks,
tedd
--
-------
http://sperling.com/
More information about the Javascript
mailing list