JavaScript DOM

Document Object Model

To create the DOM for a document, each element in the HTML is represented by what’s known as a node.

Finding an Element by ID

The most direct path to an element is via its id attribute.

HTML:
<p id="uniqueElement">
...
</p>

JavaScript:
var target = document.getElementById("uniqueElement");

Changing Styles

JavaScript:
var indigo = document.getElementById("uniqueElement");
indigo.style.backgroundColor = "#000066";

Example 1 - Example 2 (unobtrusive)

Finding Elements by Tag Name

JavaScript:
var listItems = document.getElementsByTagName("li");

Finding the Value of Attributes

Use the getAttribute method to find the value of an attribute.

HTML:
<a id="picLink" href="images/image-1.jpg" rel="lightbox" title="my caption"> image #1 </a>

JavaScript:
var theTitle = document.getElementById("picLink").getAttribute("title");

Change The Value of An Attribute

Use setAttribute to change the value of an attribute node.

JavaScript:
var shopping = document.getElementById("purchases");
shopping.setAttribute("title","a list of goods");

Image Gallery

This Image Gallery script uses getAttribute and setAttribute

function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src",source);
}

<a href="images/fireworks.jpg" onclick="showPic(this); return false;">Fireworks</a>

Image Gallery

<a href="images/fireworks.jpg" onclick="showPic(this); return false;">Fireworks</a>

The action starts when the user clicks the link

The this Keyword

this is a special JavaScript keyword that behaves like a variable, except that you can’t assign it a value—its value is the object upon which the currently-executing function was invoked as a method.

Image Gallery

We name the function and name the argument that gets passed to the function:

function showPic(whichpic)

Image Gallery

var source = whichpic.getAttribute("href");

whichpic represents the a attribute that gets passed to the function.

<a href="images/fireworks.jpg" onclick="showPic(this); return false;">Fireworks</a>

Image Gallery

var placeholder = document.getElementById("placeholder");

This assigns the image placeholder element to a variable.

<img id="placeholder" src="images/placeholder.gif" alt="my image gallery" />

Image Gallery

placeholder.setAttribute("src",source);

This updates the src attribute of the image placeholder element with the value of the href from the link we clicked.

Image Gallery

The 3 lines of code in the showPic function could have also been written as:

document.getElementById("placeholder").setAttribute("src",whichpic.getAttribute("href"));

Image Gallery

We name the function and name the argument that gets passed to the function:

function showPic(whichpic)

Image Gallery

We name the function and name the argument that gets passed to the function:

function showPic(whichpic)

Image Gallery

We name the function and name the argument that gets passed to the function:

function showPic(whichpic)

Resources