Chp05 HTML DOM
Chp05 HTML DOM
2
COMPONENTS OF DYNAMIC HTML
HTML
CSS
JavaScript
DOM.
HTML
HTML is a client-side markup language, which is a core component of the
DHTML. It defines the structure of a web page with various defined basic
elements or tags.
CSS
CSS stands for Cascading Style Sheet, which allows the web users or
developers for controlling the style and layout of the HTML elements on the
web pages. 3
CONTINUE..
JavaScript
JavaScript is a scripting language which is done on a client-side. The
various browser supports JavaScript technology. DHTML uses the
JavaScript technology for accessing, controlling, and manipulating the
HTML elements. The statements in JavaScript are the commands which
tell the browser for performing an action.
DOM
DOM is the document object model. It is a w3c standard, which is a
standard interface of programming for HTML. It is mainly used for defining
the objects and properties of all elements in HTML.
4
USES OF DHTML
It is used for designing the animated and interactive web
pages that are developed in real-time.
DHTML helps users by animating the text and images in
their documents.
It allows the authors for adding the effects on their
pages.
It also allows the page authors for including the drop-
down menus or rollover buttons.
This term is also used to create various browser-based
action games.
It is also used to add the ticker on various websites, 5
which needs to refresh their content automatically.
FEATURES OF DHTML
Its simplest and main feature is that we can create the web page
dynamically.
Dynamic Style is a feature, that allows the users to alter the font, size,
color, and content of a web page.
It provides the facility for using the events, methods, and properties.
And, also provides the feature of code reusability.
It also provides the feature in browsers for data binding.
Using DHTML, users can easily create dynamic fonts for their web sites
or web pages.
With the help of DHTML, users can easily change the tags and their
properties.
The web page functionality is enhanced because the DHTML uses low-
bandwidth effect.
6
DIFFERENCE BETWEEN HTML AND DHTML
HTML DHTML
8
EXAMPLE
<HTML>
<head>
<title>
Method of a JavaScript
</title>
</head>
<body>
<script type="text/javascript">
document.write("JavaTpoint");
</script>
</body> 9
</html>
WHAT IS HTML DOM
The HTML DOM defines a standard way for accessing and
manipulating HTML documents.
The DOM presents an HTML document as a tree-structure.
10
WHAT IS THE DOM?
The DOM is a W3C (World Wide Web Consortium)
standard.
The DOM defines a standard for accessing HTML and
XML documents:
"The W3C Document Object Model (DOM) is a platform and
language-neutral interface that allows programs and scripts to
dynamically access and update the content, structure, and style of a
document."
13
HTML DOM NODES
In the HTML DOM, everything is a node. The DOM is
HTML viewed as a node tree.
DOM Nodes
According to the W3C HTML DOM standard,
everything in an HTML document is a node:
The entire document is a document node
Every HTML element is an element node
The text inside HTML elements are text nodes
Every HTML attribute is an attribute node
Comments are comment nodes
14
HTML DOM NODE TREE
The HTML DOM views HTML documents as tree
structures. The structure is called a Node Tree:
15
HTML DOM NODE TREE
With the HTML DOM, all nodes in the tree can be
accessed by JavaScript. All HTML elements (nodes) can
be modified, and nodes can be created or deleted.
16
NODE PARENTS, CHILDREN AND
SIBLINGS
The nodes in the node tree have a hierarchical
relationship to each other.
The terms parent, child, and sibling are used to describe
the relationships. Parent nodes have children. Children
on the same level are called siblings (brothers or sisters).
In a node tree, the top node is called the root
Every node has exactly one parent, except the root (which has
no parent)
A node can have any number of children
Siblings are nodes with the same parent
17
NODE PARENTS, CHILDREN AND
SIBLINGS
The following image illustrates a part of the node tree
and the relationship between the nodes:
18
NODE PARENTS, CHILDREN AND
SIBLINGS
Look at the following HTML fragment:
19
NODE PARENTS, CHILDREN AND
SIBLINGS
From the HTML above:
The <html> node has no parent node; it is the root node
The parent node of the <head> and <body> nodes is the <html> node
The parent node of the "Hello world!" text node is the <p> node
and:
The <html> node has two child nodes: <head> and <body>
The <head> node has one child node: the <title> node
The <title> node also has one child node: the text node "DOM Tutorial"
The <h1> and <p> nodes are siblings and child nodes of <body>
and:
The <head> element is the first child of the <html> element
The <body> element is the last child of the <html> element
20
The <h1> element is the first child of the <body> element
The <p> element is the last child of the <body> element
WARNING – PROCESSING NODE
ELEMENT AND TEXT CONTENT!
A common error in DOM processing is to expect an
element node to contain text.
In this example: <title>DOM Tutorial</title>, the
element node < title>, holds a text node with the value
"DOM Tutorial".
The value of the text node can be accessed by the node's
innerHTML property.
21
HTML DOM METHODS
Methods are actions you can perform on nodes (HTML
Elements)
Programming Interface
The HTML DOM can be accessed with JavaScript (and other
programming languages).
All HTML elements are defined as objects, and the
programming interface is the object methods and object
properties .
A method is an action you can do (like add or modify an
element).
A property is a value that you can get or set (like the name 22
or content of a node).
HTML DOM OBJECTS - METHODS AND
PROPERTIES
Some commonly used HTML DOM methods:
getElementById(id) - get the node (element) with a
specified id
appendChild(node) - insert a new child node (element)
<html>
< body>
< p id="intro">Hello World!</p>
< script>
var txt=document.getElementById("intro").innerHTML;
document.write(txt);
< /script>
25
< /body>
< /html>
THE NODEVALUE PROPERTY
The nodeValue property specifies the value of a node.
nodeValue for element nodes is undefined
26
GET THE VALUE OF AN ELEMENT
27
ACCESSING HTML ELEMENTS
(NODES)
You can access an HTML element in different ways:
By using the getElementById() method (As seen before
in earlier slide)
By using the getElementsByTagName() method
getElementsByTagName() returns all elements with a
specified tag name.
28
THE GETELEMENTSBYTAGNAME()
METHOD
29
MODIFYING HTML ELEMENTS
Modifying the HTML DOM can be many
different things
Changing HTML content
Changing CSS styles
Changing HTML attributes
Creating new HTML elements
Removing existent HTML elements
Changing event(handlers)
30
CHANGING HTML CONTENT
The easiest way to change the content of an element is
by using the innerHTML property.
< script>
document.getElementById("p1").innerHTML="New text!";
< /script>
31
CHANGING HTML STYLE
With the HTML DOM you can access the style object of
HTML elements.
32
CREATING NEW HTML ELEMENTS
To add a new element to the HTML DOM, you must create the element
(element node) first, and then append it to an existing element.
33
USING EVENTS
The HTML DOM allows you to execute code
when an event occurs.
Events are generated by the browser when
"things happen" to HTML elements:
An element is clicked on
The page has loaded
Input fields are changed
34
USING EVENTS
Example 1
< input type="button"
onclick="document.body.style.backgroundColor = 'lavender';"
value="Change background color" />
Example 2
<script>
function ChangeBackground()
{
document.body.style.backgroundColor="lavender";
}
< /script>
35
< input type="button" onclick="ChangeBackground()"
value="Change background color" />
REACTING TO EVENTS (USING
JAVASCRIPT)
A JavaScript can be executed when an event occurs, like
when a user clicks on an HTML element.
To execute code when a user clicks on an element, add
JavaScript code to an HTML event attribute:
onclick=JavaScript
Some Examples of HTML events:
When a user clicks the mouse
When a web page has loaded
When the mouse moves over an element
When an input field is changed
When an HTML form is submitted
When a user strokes a key 36
THE ONMOUSEOVER AND
ONMOUSEOUT EVENTS
37
THE ONMOUSEDOWN, ONMOUSEUP
AND ONCLICK EVENTS
38