Like
for paragraph, there is a blank line before and after a
Like for italics, does not affect spacing or flow
The primary use of these tags is to hold an id attribute
With an id, we can manipulate page content
// Find thing to be replaced var mainDiv = document.getElementById("main-page"); var orderForm = document.getElementById("target");
// Create replacement var paragraph = document.createElement("p"); var text = document.createTextNode("Here is the new text."); paragraph.appendChild(text);
// Do the replacement mainDiv.replaceChild(paragraph, target);
The Document Object Model gives you access to all the elements on a web page.
The Document Object Model gives you access to all the elements on a web page.
Using any programming language, you can create, modify and remove elements in the page dynamically.
The DOM is separated into 3 different parts / levels:
Core DOM - standard model for any structured document
XML DOM - standard model for XML documents
HTML DOM - standard model for HTML documents
With the DOM, XHTML elements can be treated as objects, and many attributes of XHTML elements can be treated as properties of those objects. Then, objects can be scripted (through their id attributes) with JavaScript to achieve dynamic effects.
Warning: - The DOM is accessible only when the whole document has been loaded.That’s the reason the DOM access code is executed only after the load event has been fired.
The Document Object Collections are groups of related objects on a page.
The Document Object Collections are groups of related objects on a page.
They are array-valued properties,.
They are the properties that give you access to certain especial elements of the document:
anchors[ ]
applets[ ]
forms[ ]
images[ ]
links[ ]
To find the number of elements in the collection, use the collection’s length property . document.anchors.length returns number of
Their elements are in the same order as in the document source code.
document.forms[0] refers to the first
document.images[10] refers to the tenth tag in the document.
Access items in a collection via square brackets
Access items in a collection via square brackets
item method of a DOM collection
Access specific elements in a collection, taking an index as an argument
namedItem method
takes a name as a parameter and finds the element in the collection, if any, whose id attribute or name attribute matches it
href property of a DOM link node
Refers to the link’s href attribute
Collections allow easy access to all elements of a single type in a page
Useful for gathering elements into one place and for applying changes across an entire page
Example of Collection of type link
In the legacy DOM, with the name attribute, you can assign names to important document elements and to refer to those elements by name:
In the legacy DOM, with the name attribute, you can assign names to important document elements and to refer to those elements by name:
Assuming that the
document.forms[0] // refer to the form by position
document.f1 // refer to the form by name
document.forms[“f1”] // refer to the form as array index
Setting the name attribute of a
document.f1
You have a form that looks like this:
/* you can refer to the text input field element as : */
document.shipping.zipcode
HTML documents have a hierarchical structure of nested tags that is represented in the DOM as a tree of objects. The tree representation of an HTML document contains nodes representing HTML tags or elements, e.g and
HTML documents have a hierarchical structure of nested tags that is represented in the DOM as a tree of objects. The tree representation of an HTML document contains nodes representing HTML tags or elements, e.g and
The HTML DOM defines the objects and properties of all HTML elements, and the methods to access them.
Everything in an HTML document is a node.
The entire document is a document node
Every HTML tag is an element node
The text in the HTML elements are text nodes
Every HTML attribute is an attribute node
Comments are comment nodes
nodeName
nodeName
nodeValue
nodeType
parentNode
childNodes
firstChild
lastChild
previousSibling
nextSibling
attributes
ownerDocument
Most element nodes have child nodes:
Most element nodes have child nodes:
The node has two child nodes; and
The node has one child node; the node
The node also has one child node; the text node "DOM Tutorial“
The
and
nodes are siblings, and both child nodes of
Properties are often referred to as atributes that describes the node (i.e. nodename is "p").
Properties are often referred to as atributes that describes the node (i.e. nodename is "p").
Methods are often referred to as something that is done (i.e. delete "p").
Table contains id, text fields and corresponding method:
id = gbi, value = Get By id, method = byId()
id = ins, value = Insert Before, method = insert()
id= append, value = Append Child, method = appendNode()
id = replace, value = Replace Current, method = replaceCurrent()
Last 2 rows of the table don’t have id and text fields
Let’s now demonstrate and study the whole script in detail.
Let’s now demonstrate and study the whole script in detail.
Now you are able to take home work with you
Add a new method that can traverse from any given node even the Html node of body.
Modify the script to remove any node at the document but take care not to delete the html, head, body and script.
Modify the script to get the patent of any node
Modify the script to create new nodes with other tags than P.
Modify the script with any functionality you may find.
The objects and collections in the W3C DOM give you flexibility in manipulating the elements of a web page.
The objects and collections in the W3C DOM give you flexibility in manipulating the elements of a web page.
The W3C DOM allows you to access every element in an XHTML document. Each element in a document is represented by a separate object.
For a reference on the W3C Document Object Model, see the DOM Level 3 recommendation from the W3C at http://www.w3.org/TR/DOM-Level-3-Core/. The DOM Level 2 HTML Specification, available at http://www.w3.org/TR/DOM-Level-2-HTML/, describes additional DOM functionality specific to HTML, such as objects for various types of XHTML elements.
Not all web browsers implement all features included in the DOM specification.
You can add event handlers to HTML elements
You can add event handlers to HTML elements
You can also add handlers programmatically, from a JavaScript function:
var act = document.getElementById("act"); act.onclick = takeAction;
var images = document.getElementsByTagName("img"); for (var i = 0; i < images.length; i++) { images[i].onclick = expandImage; }
Inside expandImage, the particular image is in the variable this
Remember: JavaScript is case sensitive, HTML isn't!