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.

Table 10-4: DOM Methods to Create Nodes<br />

Method Description Example<br />

elements since they<br />

have predefined<br />

attribute names that<br />

can be manipulated<br />

directly.<br />

createComment(string) Creates an<br />

HTML/XML text<br />

comment of the form<br />

where<br />

string is the comment<br />

content.<br />

createDocumentFragment() Creates a document<br />

fragment that is useful<br />

to hold a collection of<br />

nodes for processing.<br />

createElement(tagName) Creates an element of<br />

the type specified by<br />

the string parameter<br />

tagName.<br />

createTextNode(string) Creates a text node<br />

containing string.<br />

myComment =<br />

document.createComment<br />

("Just a comment");<br />

myFragment =<br />

document.createDocument<br />

Fragment();<br />

myFragment.appendChild<br />

(temp);<br />

myHeading =<br />

document.createElement<br />

("h1");<br />

newText =<br />

document.createTextNode(<br />

"Some new text");<br />

Note <strong>The</strong> DOM Level 1 also supports document.createCDATASection(string), document<br />

.createEntity<strong>Reference</strong>(name), and document.createProcessInstruction(target,data),<br />

but these methods would not be used with typical (X)HTML documents. If CDATA<br />

sections were properly supported by browsers to mask <strong>JavaScript</strong>, however, you might<br />

see that particular method in use.<br />

Creating nodes is easy enough if you have a good grasp of (X)HTML. For example, to make a<br />

paragraph you would use<br />

newNode = document.createElement('p'); // creates a paragraph<br />

It is just as easy to make text nodes:<br />

newText = document.createTextNode("Something to add!");<br />

However, we need to join these objects together and insert them somewhere in the document<br />

in order to accomplish any interesting tasks. For now, they simply sit in memory.<br />

Inserting and Appending Nodes<br />

<strong>The</strong> Node object supports two useful methods for inserting content: insertBefore(newChild,<br />

referenceChild) and appendChild(newChild). In the case of appendChild(), it is invoked as a<br />

method of the node to which you wish to attach a child, and doing so adds the node referenced<br />

by newChild to the end of its list of children. In the case of the insertBefore() method, you<br />

specify which child you want to insert newChild in front of using referenceChild. In practice, you<br />

often have to access the parent node of the node you wish to run insertBefore() on to acquire

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

Saved successfully!

Ooh no, something went wrong!