[Javascript] Retaining values

J. Lester Novros II lester at denhaag.org
Tue Apr 12 03:59:12 CDT 2011


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
-- 
The key used for communication between the operators and the SIM card is very
well protected, because it protects their monetary interest. The other key is
less well protected, because it only protects your private data.
                                      SRLabs' Karsten Nohl on the SIMcard hack
http://lester.demon.nl/superm                    http://lester.demon.nl/tonka


More information about the Javascript mailing list