[Javascript] Retaining values

tedd tedd at sperling.com
Wed Apr 13 10:44:55 CDT 2011


At 10:47 PM -0700 4/11/11, Paul Novitski wrote:
>-snip-
>
>I recommend that you use the DOM functions createElement() and 
>appendChild() to insert the new element without stomping on what 
>you've previously inserted.
>
>document.createElement
>[1] https://developer.mozilla.org/en/DOM/document.createElement
>
>Node.appendChild
>[2] https://developer.mozilla.org/En/DOM/Node.appendChild
>
>Your script is an excellent example of the pitfalls of innerHTML. 
>It's seductively simple to use but it doesn't honor internal values 
>within the DOM, only the HTML exoskeleton.
>
>Paul

Paul:

Example [1] Uses .innerHTML, which seems to be a practice that even 
you say you shouldn't do.

Additionally, the example given fails IF the "to be inserted content" 
is enclosed within another <div>

For example:

<body onload="addElement()">
    <div id='org_div1'>
       The text above has been created dynamically.
    </div>
</body>

Will work -- see here: http://www.webbytedd.com/aa/dom-script/index1.php

Whereas:

<body onload="addElement()">
    <div id='page'>
       <div id='org_div1'>
          The text above has been created dynamically.
       </div>
    </div>
</body>

will fail -- see here: http://www.webbytedd.com/aa/dom-script

Why?

I'll discuss [2] in a following email.

Cheers,

tedd
-- 
-------
http://sperling.com/


More information about the Javascript mailing list