[Javascript] Retaining values

Paul M Foster paulf at quillandmouse.com
Tue Apr 12 08:49:28 CDT 2011


On Tue, Apr 12, 2011 at 10:59:12AM +0200, 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].

Thanks for this. I've often wondered about the wisdom of innerHTML, but
I've never seen any official warnings about it. Nice to see real world
confirmation.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com


More information about the Javascript mailing list