23.04.2013 Views

javascript

javascript

javascript

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.

Creating Element or Text Nodes<br />

CHAPTER 7 ■ TRAVERSING AND MODIFYING THE DOM TREE<br />

Oftentimes, you will want to create Element or Text nodes with JavaScript and then insert them into the<br />

DOM tree. Doing so is one way to dynamically add content to a web page.<br />

One of the implications of DOM being defined with interfaces rather than classes is that you cannot,<br />

for example, create an Element node by writing something like this:<br />

var myLI = new Element("li");<br />

Rather than creating DOM nodes with constructor functions, you do so with factory methods from<br />

the Document interface. So let’s try that. Click Clear in both Firebug panels, and create an Element node,<br />

say an , with the factory method Document.createElement():<br />

var myLI = document.createElement("li");<br />

That was pretty straightforward. Just pass Document.createElement() the tag name of the element as<br />

a string, and this factory method returns a new Element node to you. But it’s blank; it has no attributes<br />

other than defaults from the DTD. It contains no child nodes either. So, you have some work to do<br />

before adding the to the DOM tree. Let’s tackle attributes first. You already know how—set id to<br />

"blog" and class to "sprite":<br />

var myLI = document.createElement("li");<br />

myLI.id = "bebo";<br />

myLI.className = "sprite";<br />

Now it’s time for the children. The existing four elements have a child element with a child<br />

Text node. That’s what you want this new to have, too. Create the first. Do it the same way as<br />

you did the . Then add an href attribute with a value of "http://www.bebo.com".<br />

var myLI = document.createElement("li"), myA = document.createElement("a");<br />

myLI.id = "bebo";<br />

myLI.className = "sprite";<br />

myA.href = "http://www.bebo.com";<br />

Now for the Text node. Like the element factory method, this one is defined by the Document<br />

interface, too. But be wary, unlike the element factory method, the identifier for this one ends with Node:<br />

createTextNode, not createText. Document.createTextNode() works with just one parameter, which is the<br />

string of text you want the node to represent.<br />

var myLI = document.createElement("li"),<br />

myA = document.createElement("a"),<br />

myText = document.createTextNode("Bebo");<br />

myLI.id = "bebo";<br />

myLI.className = "sprite";<br />

myA.href = "http://www.bebo.com";<br />

Now you have two Element nodes and one Text node floating around in the ether. How do you insert<br />

those into the DOM tree? Well, every kind of node, yup all 12 of ’em, has three methods to do so:<br />

Node.appendChild()<br />

Node.insertBefore()<br />

Node.replaceChild()<br />

What do those do? The first one, Node.appendChild(), appends the node you pass to it to the end of<br />

the childNodes array of the node you invoke it upon. Invoke Node.appendChild() on myA, passing myText<br />

as the parameter:<br />

var myLI = document.createElement("li"),<br />

289

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

Saved successfully!

Ooh no, something went wrong!