Unit 5 DOM and DOM Event handling
Unit 5 DOM and DOM Event handling
Assistant Professor
Department of E & TC
In client-side JavaScript, an object model is the interface to the various aspects of the
browser and the document that can be manipulated in code.
Over time the object models supported in browsers have evolved, but for simplicity, we
may break down object models by browser type and version.
We also note that object models may focus on accessing the features and characteristics
of a
When we write
document.write() or
Document.getElementById()
We are invoking document objects
HTML page Window
<!DOCTYPE html>
<html>
<head> Hello </head> Document
<body>
<h2>JavaScript While Loop</h2>
<p id="demo"></p> HTML
<script>
let text = "";
let i = 0; head body
while (i < 10)
{
text += "<br>The number is " + i;
h2 p scrip
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Why DOM??
HTML is used to structure the webpage and JS is used to add behaviour to the webpage.
The moment you load the HTML file into browser;
JS does not understand the HTML document. It does not understand the tags.
Thus, a corresponding document (DOM) is created.
DOM is basically the representation of the same HTML document but in a different format
with the use of objects.
Javascript interprets DOM easily i.e javascript can not understand the tags(<h1>H</h1>) in
HTML document but can understand object h1 in DOM. Now, Javascript can access each of
the objects (h1, p, etc) by using different functions.
Using DOM, programmers can build, construct, add, modify, delete, or navigate elements or
contents. Everything present on an HTML webpage can be edited using DOM.
DOM is not a programming language but it the way you access or modify data. DOM is a
logical structure of a JS program.
DOM Structure:
DOM can be thought of as a Tree or Forest(more than one tree).
Tree will have nodes, Children, siblings, leaf nodes etc
DOM Flavors
Four levels of the DOM are defined as follows
DOM Level 0 Roughly equivalent to what Netscape 3.0 and Internet Explorer 3.0 supported.
We call this DOM the classic, or traditional, JavaScript object model. This form of the DOM
was presented in slide no 5 and supports the common document object
collections—forms[], images[], anchors[], links[], and applets[].
DOM Level 1 Provides the ability to manipulate all elements in a document through a common
set of functions. In DOM Level 1, all elements are exposed, and parts of the page can be read
and written to at all times. The Level 1 DOM provides capabilities similar to Internet
Explorer’s, except that it is cross-browser–compatible and standardized.
DOM Level 2 Provides further access to page elements primarily related to CSS and focuses
on combining DOM Levels 0 and 1 while adding improved support for working with XML
documents. This form of the DOM also adds an advanced event model and the lesser-known
extensions such as traversal and range operations.
DOM Level 3 Made some modifications to the core facilities provided by DOM Levels 1 and 2,
and introduced a number of sparsely implemented features such as XML Load and Save.
Some of the more practical parts of DOM Level 3 live on in the HTML5 specification.
DOM Categories
There are five categories in DOM
DOM Core Specifies a generic model for viewing and manipulating a marked up
document as a tree structure.
DOM HTML Specifies an extension to the core DOM for use with HTML. DOM
HTML provides the features used to manipulate HTML documents and utilizes a
syntax similar to the traditional JavaScript object models. Basically, this is DOM Level
0 plus capabilities for manipulating all of the HTML element objects.
DOM CSS Provides the interfaces necessary to manipulate CSS rules
programmatically.
DOM Events adds event handling to the DOM. These events range from familiar user
interface events such as mouse clicks to DOM-specific events that fire when actions
occur that modify parts of the document tree.
DOM XML Specifies an extension to the core DOM for use with XML. DOM XML
addresses the particular needs of XML, such as CDATA sections, processing
instructions, namespaces, and so on
Document Tree
Finding HTML elements by tag name: The HTML DOM getElementsByTagName() method is
used to access the HTML elements using the tag name. This function takes single parameter.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM getElementsByTagName</h2>
<p>Finding HTML Elements by Tag Name.</p>
<p id="demo"></p>
<script>
const element = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = 'The text in first paragraph is: ' +
element[0].innerHTML;
</script>
</body>
</html>
Finding HTML elements by class name
The getElementsByClassName() method in Javascript is used to access HTML elements using
classname. Each element in the returned object can be accessed by its index. The index value will
start with 0.
<!DOCTYPE html> Output:
<html>
<body>
<div class="example">First</div>
<div class="example">Second</div>
Output: at index 0
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
var x = document.getElementsByClassName("example");
x[0].innerHTML = "Hello World!"; Output: at index 1
}
</script>
</body>
</html>
Finding HTML elements by CSS selectors
What is CSS selector?
Consider in HTML we define a div in body as
<body>
<div class =“box”></div>
</body>
CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule
set.
CSS selectors select HTML elements according to its id, class, type, attribute etc.
There are several different types of selectors in CSS.
1. CSS Element Selector
CSS
<html>
<body> H1
{
<h1> hello </h1> styling
}
<p> this is para </p> P
</body> {
styling
</html> }
Finding HTML elements by CSS selectors (Simple Selector)
1. CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An
id is always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
HTML CSS
<html> H1
<body> {
styling
<h1> hello </h1> }
<p> this is para </p> P
{
<div id = “demo” hello! </div> styling
</body> }
#demo
</html> {
}
Finding HTML elements by CSS selectors (Simple Selector)
CSS Class Selector
The class selector selects HTML elements with a specific class attribute. It is used with a
period character . (full stop symbol) followed by the class name.
HTML CSS
<html> H1
<body> {
styling
<h1> hello </h1> }
<p> this is para </p> P
{
<div class = “demo” hello! </div> styling
</body> }
.demo
</html> {
}
Finding HTML elements by CSS selectors (Simple Selector)
CSS Universal Selector
The universal selector is used as a wildcard character. It selects all the elements on the pages.
HTML CSS
<html> *{
<body> margin =0;
Padding = 0;
<h1> hello </h1> }
<p> this is para </p> The above styling will be for all
the elements in HTML
<div class = “demo” hello! </div>
</body>
</html>
Finding HTML elements by CSS selectors (Simple Selector)
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate;
</body>
</html>
onchange Event
<html>
<body>
<p>Select your favorite fruit from the list.</p>
<select id="mySelect" onchange="myFunction()">
<option value="mango">mango</option>
<option value="apple">apple</option>
<option value="grapes">graps</option>
<option value="pear">pear</option>
</select>
<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
DOM2 Event Model
There are two ways of event propagation in the HTML DOM, bubbling and capturing.
Event propagation is a way of defining the element order when an event occurs. If
you have a <p> element inside a <div> element, and the user clicks on the <p>
element, which element's "click" event should be handled first?
In bubbling the inner most element's event is handled first and then the outer: the
<p> element's click event is handled first, then the <div> element's click event.
In capturing the outer most element's event is handled first and then the inner: the
<div> element's click event will be handled first, then the <p> element's click event.
With the addEventListener() method you can specify the propagation type by using
the "useCapture" parameter:
The default value is false, which will use the bubbling propagation, when the value is
set to true, the event uses the capturing propagation.
Handling events in DOM2 : addEventListener()
The methods used for event handling in DOM 2 are listed below
addEventListener() and removeEventListener()