[Javascript] Retaining values

tedd tedd at sperling.com
Wed Apr 13 11:07:17 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:

Ok, [2] -- let's deal with insertions, because that's what this is 
about, namely inserting code within existing code (It reminds me of 
the old self generating code we used to write back in the stone age).

Using:

    p = document.createElement("p");
    document.body.appendChild(p);

Will create and insert <p></p> tag within the body tags of a web page.

And using:

    p = document.createElement("p");
    document.getElementsById('placeholder').appendChild(p);

Will create and insert '<p></p>' tag between a 'placeholder' <div>, such as:

<div id='placeholder'>
<p></p>
</div>

But how do you insert something between the <p></p> tags?

And while you're at it, how do you place '\n' at the end of these 
insertions so that the resultant code aren't run-ons? I hate seeing a 
long line of code without linefeeds -- makes my head hurt. :-)

Cheers,

tedd

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


More information about the Javascript mailing list