JavaScript Editor JavaScript Debugger     JavaScript Editor 

Team LiB
Previous Section Next Section

The DOM and HTML Elements

Now that we have presented both how to create (X)HTML elements and how to set and manipulate attributes, it should be clear how very intertwined markup and JavaScript have become as a result of the DOM. In short, to effectively utilize the DOM, you must be an expert in (X)HTML syntax, since many object properties are simply direct mappings to the attributes of the (X)HTML element. For example, the paragraph element defined under HTML 4.01 has the following basic syntax:

<<p align="left | center | right | justify"
     id="unique id"
     class="class name"
     style="style rules"
     title="advisory text"
     lang="language code"
     dir="text direction either LTR or RTL">>
   paragraph content

DOM Level 1 exposes most of these attributes in the HTMLParagraphElement, including align, id, className, title, lang, and dir. DOM Level 2 also exposes style, which we’ll discuss in the next section. The various event handlers, such as onclick and onmouseover, are also settable (through mechanisms discussed in the next chapter).

All HTML element interfaces derive from the basic HTMLElement object that defines id, className, title, lang, and dir. Many HTML elements do not support any other attributes. Such elements include

Beyond the core attributes, the rest of an element’s properties follow (X)HTML syntax. In fact, if you are already intimately familiar with (X)HTML, it is fairly easy to guess the DOM properties that correspond to HTML element attributes by following these basic rules of thumb. If the attribute is a simple word value like “align,” it will be represented without modification unless the word conflicts with JavaScript reserved words. For example, the <<label>> tag, defined by HTMLLabelElement, supports the for attribute, which would obviously conflict with the for statement in JavaScript. To rectify this, often the word “html” is prepended, so in the previous case the DOM represents this attribute as htmlFor. In a few other cases, this rule isn’t followed. For example, for the <<col>> tag, attributes char and charoff become ch and chOff under the DOM Level 1. Fortunately, these exceptions are few and far between. And finally, if the attribute has a two-word identifier such as tabindex, it will be represented in the DOM in the standard JavaScript camel-back style, in this case as tabIndex.

The only major variation in the HTML-to-DOM mapping is with tables. Given the increased complexity of tables under HTML 4.0, there are numerous methods to create and delete various aspects of tables, such as captions, rows, and cells, as well as HTML 4.0 tags like <<tfoot>>, <<thead>>, and <<tbody>>. These are all detailed in Appendix B and are demonstrated in Chapter 13.

Last, in order to support traditional JavaScript programming syntax, you will find a number of methods and properties of the form element itself as well as the various form field elements like input, select, textarea, and button. We’ll discuss form manipulation in-depth in Chapter 14.

As a brief demonstration of just what can be done with the DOM, the following example demonstrates a very simple HTML creation tool using DOM methods.

<<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<<html xmlns="">>
<<title>>DOM HTML Editor 0.1<</title>>
<<meta http-equiv="content-type" content="text/html; charset=utf-8" />>
<<script type="text/javascript">>

function addElement()
 var choice = document.htmlForm.elementList.selectedIndex;
 var theElement =
 var textNode = document.createTextNode(document.htmlForm.elementText.value);
 var insertSpot = document.getElementById('addHere');


function addEmptyElement(elementName)
 var theBreak = document.createElement(elementName);
 var insertSpot = document.getElementById('addHere');

function deleteNode()
 var deleteSpot = document.getElementById('addHere');
 if (deleteSpot.hasChildNodes())
   var toDelete = deleteSpot.lastChild;

function showHTML()
 var insertSpot = document.getElementById('addHere');
 if (insertSpot.innerHTML)
   alert("Not easily performed without innerHTML");

<<h1 style="text-align: center;">>Simple DOM HTML Editor<</h1>>
<<br />><<br />>
<<div id="addHere" style="background-color: #ffffcc; border: solid;">>
<<br />><<br />>

<<form id="htmlForm" name="htmlForm" action="#" method="get">>
<<select id="elementList" name="elementList">>

<<input type="text" name="elementText" id="elementText" value="Default" />>
<<input type="button" value="Add Element" onclick="addElement();" />>

<<br />><<br />>
<<input type="button" value="Insert <<br>>" onclick="addEmptyElement('BR');" />>
<<input type="button" value="Insert <<hr>>" onclick="addEmptyElement('HR');" />>
<<input type="button" value="Delete Element" onclick="deleteNode();" />>
<<input type="button" value="Show HTML" onclick="showHTML();" />>

It would be easy enough to modify the editor displayed in Figure 10-4 to add attributes and apply multiple styles. We’ll leave that as an exercise for readers interested in diving into the DOM.

Click To expand
Figure 10-4: Simple DOM-based HTML editor

Appendix B provides a complete presentation of all (X)HTML elements and properties under DOM Levels 1 and 2. For more information on (X)HTML syntax, see the companion book HTML & XHTML: The Complete Reference, Fourth Edition by Thomas Powell (Osborne/McGraw-Hill, 2003) (, or visit the W3 site at

Team LiB
Previous Section Next Section

JavaScript Editor JavaScript Debugger     JavaScript Editor