11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the necessary references. Let‘s see the appendChild() method in action, by using it to combine<br />

the two nodes that we create.<br />

newNode = document.createElement('b'); newText = document.createTextNode("Something to<br />

add!");newNode.appendChild(newText);<br />

At this point we would have this (X)HTML fragment:<br />

Something to add<br />

We could then add this markup into the document once we have found a convenient place to<br />

insert it. For example, we might use<br />

current = document.getElementById('p1');current.appendChild(newNode);<br />

to append the bold text fragment to the end of our test paragraph. <strong>The</strong> following example<br />

demonstrates a more complex use of insert and append that places user-entered text before,<br />

within, and after a specified element:<br />

Note If you have never seen DOM functionality before, you are highly encouraged to try this<br />

example yourself. You can type it in manually or find it online at the support site for this<br />

book, www.javascriptref.com.<br />

<br />

<br />

<br />

DOM Adding<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!