Document Object Model - DOM

Sample HTML Page

<!DOCTYPE html>
<html>
    <head>
        <title>Sample page</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

Sample HTML DOM

document
Document Node
<!DOCTYPE html>
Document Type Node
<html>
Element Node
<head>
Element Node
<title>
Element Node
Sample Page
Text Node
<body>
Element Node
<h1>
Element Node
Hello World
Text Node

Node type

Chick the type by doing node.nodeType

node.nodeType === Node.ELEMENT_NODE
List of node types

Node name

node.nodeName === 'H1'

Text node

node.nodeType === Node.TEXT_NODE // 3
node.nodeValue // get the text value

Node Parents, Children, and Siblings

Example


Element Attribute

node.getAttribute('attribute_name')
node.setAttribute('attribute_name', value)

Find elements in the DOM

Example


Create nodes

Add/remove nodes

innerHTML

Returns the inner HTML as a string

Example


Change style/class


Template Example


Events


Animation


AJAX


Rouben Meschian

rmeschian@gmail.com