Module 5 DOM
Module 5 DOM
Model
Module 5
Document Object Model
The components of web pages are represented by objects that are organized in a
hierarchical structure called Document Object Model (DOM)
DOM is a programming interface for HTML and XML documents.
It defines the logical structure of documents and the way a document is accessed
and manipulated
The hierarchical structure is represented as a parent-child relationship
Child objects are basically properties of their parent objects
These objects have properties and methods that can be used to work with web page
Document Object
The document object contains information about the current HTML document
displayed on the window.
An HTML document is represented as a DOM tree by the browser.
The browser traverses the DOM tree and renders the document on the screen
The document object has many properties like bgcolor, fgcolor, forms, title, URL etc.
It has methods like getElementById, createElement, createAttribute etc.
DOM: Example
<html>
<head>
<title>My title</title>
</head>
<body>
<a href=“abc.html”>My link</a>
<h1>My header</h1>
</body>
</html>
JavaScript has the capability to access and manipulate elements from the window and
document.
The top level object window and all the direct child objects (E.g. document,
navigator, history etc.) can be accessed directly
Any other object is referenced by its name prefixed by names of all parent objects in
order separated by dots(.)
For example:
document.body.p
Methods to access HTML elements:
document.getElementById(ID): returns reference to the element with given ID
document.getElementsByTagName(name): returns collection of all element of the
given tag name
Image Object
The Image Object in HTML DOM is used to represent the HTML < image > element
Creating image
The Image() constructor creates a new HTML image element instance.
Example:
const myImage = new Image(100, 200); //Image(width, height)
myImage.src = "picture.jpg";
document.body.appendChild(myImage);
The above code is equivalent to
<img src= “picture.jpg” width=100 height=200>
The image element can be accessed by using the getElementById() method
var x = document.getElementById(“myImg”);
Keyboard Keydown & Keyup onkeydown & onkeyup When the user press and then release the key
load onload When the browser finishes the loading of the page
resize onresize When the visitor resizes the window of the browser
Example (focus event):
Form Data Validation
When you enter data, the browser and/or the web server will check to see that the
data is in the correct format and within the constraints set by the application.
If the information is correctly formatted, the application allows the data to be
submitted to the server and (usually) saved in a database;
If the information isn't correctly formatted, it gives the user an error message
explaining what needs to be corrected and lets them try again.
This is called form validation
JavaScript provides the facility to validate the form in the browser on the client side.
Submit event
The onSubmit event is typically associated with HTML forms.
It is triggered when a user submits a form by clicking the submit button or pressing
the Enter key while focusing on a form field.
The onSubmit event is crucial for form validation, data submission, and other form-
related functionalities.
Example: