JavaScript DOM
JavaScript DOM
DOM stands for Document Object Model. When a browser loads a web page, the browser
creates a Document Object Model of that page. The HTML DOM is created as a tree of
Objects.
The HTML DOM is a standard for how to get, change, add, or delete HTML
elements.
Example:
<html>
<head>
<title>My Page Title</title>
</head>
<body>
<script type="text/javascript">
</script>
<div>
<h1>This is browser DOM</h1>
</div>
</body>
</html>
HTML DOM
For this HTML a graphical representation of the Document Object Model is
shown below.
HTML DOM
With the object model, JavaScript gets all the power it needs to create
dynamic HTML:
•JavaScript can change all the HTML elements in the page
•JavaScript can change all the HTML attributes in the page
•JavaScript can change all the CSS styles in the page
•JavaScript can remove existing HTML elements and attributes
•JavaScript can add new HTML elements and attributes
•JavaScript can react to all existing HTML events in the page
•JavaScript can create new HTML events in the page
DOM Methods
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello
World!";
</script>
</body>
</html>
• Document.write()
DOM Forms
Passing Parameters
Event Bubbling or Event Capturing?
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:
addEventListener(event, function, useCapture);
The default value is false, which will use the bubbling propagation, when the value is set
to true, the event uses the capturing propagation.
Event Bubbling or Event Capturing?
<!DOCTYPE html>
<html> <script>
<head> document.getElementById("myP1").addEven
<style> tListener("click", function() {
#myDiv1, #myDiv2 { alert("You clicked the white element!");
background-color: coral; }, false);
padding: 50px;
} document.getElementById("myDiv1").addEv
#myP1, #myP2 { entListener("click", function() {
background-color: white; alert("You clicked the orange element!");
font-size: 20px; }, false);
border: 1px solid;
padding: 20px; document.getElementById("myP2").addEven
} tListener("click", function() {
</style> alert("You clicked the white element!");
<meta content="text/html; charset=utf-8" }, true);
http-equiv="Content-Type">
</head> document.getElementById("myDiv2").addEv
<body> entListener("click", function() {
<h2>JavaScript addEventListener()</h2> alert("You clicked the orange element!");
<div id="myDiv1"> }, true);
<h2>Bubbling:</h2> </script>
<p id="myP1">Click me!</p>
</div><br> </body>
<div id="myDiv2"> </html>
<h2>Capturing:</h2>
<p id="myP2">Click me!</p>
</div>
Event Bubbling or Event Capturing?
<!DOCTYPE html>
<html>
DOM Event Listener (Remove)
<head>
<style> <script>
#myDIV { document.getElementById("myDIV").addEventList
background-color: coral; ener("mousemove", myFunction);
border: 1px solid;
padding: 50px; function myFunction() {
color: white; document.getElementById("demo").innerHTML =
font-size: 20px; Math.random();
} }
</style>
</head> function removeHandler() {
<body>
document.getElementById("myDIV").removeEvent
<h2>JavaScript removeEventListener()</h2> Listener("mousemove", myFunction);
}
<div id="myDIV"> </script>
<p>This div element has an onmousemove
event handler that displays a random number </body>
every time you move your mouse inside this </html>
orange field.</p>
<p>Click the button to remove the div's
event handler.</p>
<button onclick="removeHandler()"
id="myBtn">Remove</button>
</div>
<p id="demo"></p>
DOM Event Listener(Remove)
Image gallery with thumbnails in JavaScript
The image gallery should be as shown in the image below. When you click on the image
thumbnail, the respective image should be displayed in the main section of the page.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.imgStyle
{
width:100px;
height:100px;
border:3px solid grey;
}
</style>
</head>
<body>
<img id="mainImage" style="border:3px solid grey"
src="Images/1.jpg" height="500px" width="540x"/>
<br />
<div id="divId" onclick="changeImageOnClick(event)">
<img class="imgStyle" src="Images/1.jpg" />
<img class="imgStyle" src="Images/2.jpg" />
<img class="imgStyle" src="Images/3.jpg" />
<img class="imgStyle" src="Images/4.jpg" />
<img class="imgStyle" src="Images/5.jpg" />
</div>
<script type="text/javascript">
if (targetElement.tagName == "IMG")
{
mainImage.src = targetElement.getAttribute("src");
}
}
</script>
</body>
</html>