27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Even More DOM<br />

In the previous exercise, you have created the list of elements by joining strings to compose a<br />

simple HTML structure. The same HTML structure can be built programmatically using the<br />

DOM. In the next exercise, you will generate this content programmatically:<br />

<br />

Hello Dude! Here's a cool list of colors for you:<br />

<br />

<br />

Black<br />

Orange<br />

Pink<br />

<br />

<br />

Chapter 2<br />

A DOM document is a hierarchical structure of elements, where each element can have one or<br />

more attributes. In this HTML fragment, the single element with an attribute is , which has<br />

an attribute called id with the value myDivElement. The root node that you can access through the<br />

document object is . When implementing the above HTML document, you will end up with<br />

a structure such as the one in the figure below:<br />

Figure 2.3: A Hierarchy of HTML Elements<br />

In Figure 2.3, you see an HTML structure formed of , , , , <strong>and</strong> <br />

elements, <strong>and</strong> four text nodes ("Hello…", "Black", "Orange", "Pink"). In the next exercise, you will<br />

create this structure using the DOM functions createElement, createTextNode, <strong>and</strong> appendChild.<br />

Time for Action—Even More DOM<br />

1. In the foundations folder, create a subfolder called evenmorejsdom.<br />

2. In the evenmorejsdom folder, create a file called evenmorejsdom.html, <strong>and</strong> add the<br />

following code to it:<br />

<br />

<br />

<br />

<strong>AJAX</strong> Foundations: Even More JavaScript <strong>and</strong> DOM<br />

<br />

<br />

37<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!