Readings:
W3Schools DOM Reference.
Instant lab: redot.html.
- The Document Object Model represents the current page to Javascript.
- Each element in the document is represented by an object of type
Element.
- Elements objects contain each other as elements in HTML, forming a tree.
- The DOM tree is built by the browser when it reads the HTML document.
- Some of the HTML translated into the tree
may come from document.write instead of
HTML text.
- Javascript can use the DOM to access or modify the document.
- The object will have properties corresponding to the attributes
on the tag.
- Getting the element object you need.
- Events specified in HTML can pass this.
- Event objects contain a currentTarget attributes.
- document.getElementById("idname")
- Returns the element object having the specified identifier.
- Probably the most common way to get the element you want.
- elt.getElementsByClassName() and
elt.getElementsByTagName()
- Returns a node list, an array-like object.
- Can be run on any node, and searches only its contents.
- Given an element, you may wander around the document tree
(see here).
- elt.parentNode Going up.
- elt.childNodes Returns a node list giving
all the child nodes, in the order listed in the document.
- elt.firstChild Gives the first child, in document order.
- elt.nextSibling The sibling following.
- And many more.
- Attributes and Properties.
(See here.)
- Properties are object data.
- You can fetch or set them with the usual dot notation.
var x = obj.prop; obj.prop = 'value';
- Properties have whatever type you assign.
- Attributes represent the attributes in the HTML tags.
- You can fetch them with obj.getAttribute("name"), or set with
obj.setAttribute("name","value").
- Values are always strings.
- Generally, setting one sets the other.
- There seem to be some exceptions.
- Don't think setting a property will create a corresponding
attribute.
- I'm quite vague about all these details.
- You can set custom attributes in HTML, which are available to
Javascript.
- Attributes that start with data- are for programmers use;
will not become standard in the future.
- Can modify the document by setting the the properties or attributes.
[ Tabs Source ]
[ Image Index Source ]
- Updating the tree
- elt.innerHTML
- The HTML code contained in the element (excluding the element itself).
- Can be fetched or assigned.
- Simple way to replace a section of the document.
- Works nicely on a div.
[ Client-Side Guessing Game Source
Support JS ]
- Note that Javascript is event-driven: Most of the time, nothing is
running. Code runs when something happens.
- For this version of the game, you can cheat
using the javascript console under
development tools.
- elt.textContent
- Represents the contained text, ignoring tags.
- Most useful for spans and such.
- Can be fetched or set. Setting will destroy any existing structure.
- You can update the tree directly.
- Use var elt = document.createElement("tag", options)
to create new element objects.
- Options is mostly useless.
- Generally use elt.setAttribute() after creating it.
- Insert the created element into the tree.
- elt.appendChild(newchild); Add a new child as the last
descendant.
- elt.insertBefore(newnode, existingchild); Add a new child
before a specified existing child.
- elt.removeChild(existingchild); Just what it says.
[ Using the event object. Source ]
- Manipulating forms.
- Form elements are stored in the form in properties given by their
own name attributes.
- The value attribute takes on what the user has typed.
- The onSubmit() can vet the form data and prevent submission.
[ Basic Form With Checks Source
Processor ]