Document Object Model
Document Object Model
Document Object Model
MODEL
CS 109e
What is the DOM?
• DOM stands for Document Object Model?
• An API for HTML and XML
• Defines the logical structure of documents
• Defines the way a document is accessed
• Defines the way a document is manipulated
• Closely resembles the structure of the document it
models
The W3C DOM
Donato, Alforja,
Cabuyao Kent Calamba
Michelle
Object Modeling
• Documents are modelled using objects.
• The model encompasses both structure and
behaviour of which it is composed.
• In the diagram (DOM representation of <table>),
the nodes do not represent a data structure but
objects – which have functions and identity.
Trees, Forest
Example:
var x =
document.getElementsByTagName("p");
Example:
var x = document.getElementsById("
main");
var x = x.getElementsByTagName("p");
Example
var x = document.getElementsByClassName("intro");
Finding HTML Elements by CSS Selectors
If you want to find all HTML elements that matches a specified CSS selector (id, class
names, types, attributes, values of attributes, etc), use the querySelectorAll()
method.
This example returns a list of all <p> elements with class="intro".
Example
var x = document.querySelectorAll("p.intro");
Finding HTML Elements by HTML Object
Collections
This example finds the form element with id="frm1", in the forms
collection, and displays all element values:
Example
var x = document.forms["frm1"];
var text = "";
var i;
for (i = 0; i < x.length; i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
The following HTML objects (and object collections) are
also accessible:
document.anchors
document.body
document.documentElement
document.embeds
document.forms
document.head
document.images
document.links
document.scripts
document.title
Changing the HTML Output Stream
JavaScript can create dynamic HTML content:
Date: Tue Aug 17 2017 11:00:42 GMT+0800 (Philippine
Standard Time)
In JavaScript, document.write() can be used to write directly to the
HTML output stream:
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write(Date());
</script>
</body>
</html>
Changing the HTML Output Stream
Never use document.write() after the
document is loaded. It will overwrite the
document.
Changing the Value of an Attribute
To change the value of an HTML attribute,
use this syntax:
document.getElementById(id).attribute = new
value
Changing the Value of an Attribute
This example changes the value of the src attribute of an <img>
element:
Example
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Changing HTML Elements
Method Description
element.innerHTML = new html Change the inner
content HTML of an element
element.attribute = new value Change the attribute
value of an HTML
element
element.setAttribute(attribute, value) Change the attribute
value of an HTML
element
element.style.property = new style Change the style of an
Adding and Deleting Elements
Method Description
• convert() accepts two parameters, the form and the XML island.
An Application for DOM (Example)
<SCRIPT LANGUAGE="JavaScript"
SRC="conversion.js"></SCRIPT>
function convert(form, xmldocument)
{
var fname = form.fname.value,
output = form.output,
rate = form.rate.value;
output.value = "";
var document = parse(fname, xmldocument),
topLevel = document.documentElement;
searchPrice(topLevel, output, rate);
}
An Application for DOM
function parse(uri,xmldocument)
{
xmldocument.async = false;
xmldocument.load(uri);
if (xmldocument.parseError.errorCode != 0)
alert(xmldocument.parseError.reason);
return xmldocument;
}
An Application for DOM
function searchPrice(node,output,rate)
{if(node.nodeType == 1)
{if(node.nodeName == "price")
output.value += (getText(node) * rate) + "\r";
var children,
i;
children = node.childNodes;
for(i = 0;i < children.length;i++)
searchPrice(children.item(i),output,rate);}}
function getText(node)
{return node.firstChild.data
An Application of DOM
• nodeType is a code representing the type of the object.
• parentNode is the parent (if any) of current Node object.
• childNode is the list of children for the current Node object.
• firstChild is the Node’s first child.
• lastChild is the Node’s last child.
• previousSibling is the Node immediately preceding the current one.
• nextSibling is the Node immediately following the current one.
• attributes is the list of attributes, if the current Node has any.
An Application of DOM