Unit 2 DOM
Unit 2 DOM
(DOM)
Document Object Model
• the web browser creates a DOM of the webpage when the page is
loaded.
• The backbone of an HTML document are tags.
• According to Document Object Model (DOM), every HTML-tag is an
object. Nested tags are called “children” of the enclosing one.
• JavaScript can access all the elements in a webpage making use of
Document Object Model (DOM).
• The DOM model is created as a tree of objects like this:
The DOM is often referred to as the DOM tree, and consists of a tree of
objects called nodes.
• The DOM creates a hierarcy corresponding to the structure of each web
document. This hierarchy is made up of nodes. There are several
different types of DOM nodes, the most important are 'Element', 'Text'
and 'Document’.
<a href="index.html">Home</a>
Everything between the opening and closing tag combined make the entire HTML element.
Any alone text outside of an element is a text node, and an HTML comment is a comment
node.
• HTML DOM methods are actions you can perform (on HTML
Elements).
• HTML DOM properties are values (of HTML Elements) that you can
set or change.
The Document Object Model (DOM) is essential in web development for several reasons:
Dynamic Web Pages: It allows you to create dynamic web pages. It enables the JavaScript to
access and manipulate page content, structure, and style dynamically which gives
interactive and responsive web experiences, such as updating content without reloading
the entire page or responding to user actions instantly.
Interactivity: With the DOM, you can respond to user actions (like clicks, inputs, or scrolls)
and modify the web page accordingly.
Content Updates: When you want to update the content without refreshing the entire page,
the DOM enables targeted changes making the web applications more efficient and user-
friendly.
Cross-Browser Compatibility: Different browsers may render HTML and CSS in different
ways. The DOM provides a standardized way to interact with page elements.
With the object model, JavaScript gets all the power it needs to create
dynamic HTML:
Method Description
write("string") writes the given string on the doucment.
<form name="form1">
Enter Name:<input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/>
</form>
document.getElementById() method
<script type="text/javascript">
function getcube(){
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form
document.getElementsByName() method
The getElementsByName() method is use to get the element by name. However be
aware of the getElementsByName() method always return an array as output. Since
same name can belongs to multiple elements.
<script type="text/javascript">
function getcube(){
var number=document.getElementsByName("number")[0].value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
Document.getElementsByTagName()
<head>
<script>
function cntpara() {
var allParas = document.getElementsByTagName('p');
var num = allParas.length;
alert('There are ' + num + ' paragraph in this document');
}
</script>
</head>
<body>
<p>B.Tech IV Sem </p>
<p>Last Date of submitting Project is 15 April 2024.</p>
<button onclick="cntpara();">
Click to Count</button><br />
</body>
</html>
Javascript - innerHTML
</html>
Changing CSS Properties with DOM
function showtext()
<html>
{
<head>
var x =document.getElementById("p1");
<script>
x.style.visibility='visible';
function changepos()
{
}
var x =document.getElementById("p1");
</script>
x.style.color='red';
</head>
<body >
}
<p id="p1"> Hi All !! </p>
<input type="button" onclick="changepos()">
function showhide()
<input type="button" value="Hide" onclick="showhide()">
{
<input type="button" value="show" onclick="showtext()">
var x =document.getElementById("p1");
x.style.visibility='hidden';
</body>
</html>
}
DOM Events
The index starts at zero and extends to however many items have been added, which is a
property of the array known as the "length" of the array.
• By array literal
• By creating instance of Array directly (using new keyword)
• By using an Array constructor (using new keyword)
1. By Array Literal
var arrayname=[value1,value2.....valueN];
<script>
var a = ["emp1", "emp2", "emp3"];
<script>
var i;
var a = new Array();
a[0] = "emp1";
a[1] = "emp2";
<script>
var a = new Array("emp1", "emp2", "emp3");
for (i=0; I < a.length; i++)
{
document.write(a[i] + "<br>");
}
</script>
Types of values in array elements
In most high-level programming languages arrays are typed. This means that when an array is
created the programmer must specify the type of the value to be stored in the array.
With such languages, all elements of an array store values of a single type.
However, JavaScript is not a strongly typed programming language, and a feature of JavaScript
arrays is that a single array can store values of different types.
<html>
<body>
<script>
var a = [12,"Hello", 34.2];
for(i=0 ; i<a.length; i++)
{
document.write(a[i]);
}
</script>
</body>
</html>
Array Methods
concat()
The concat() method is represented by the following syntax:
array.concat (arr1,arr2,....,arrn)
<script>
var a = ["red", "blue"];
var b = ["circle", "square", "triangle"];
var result = a.concat(b);
document.write(result);
</script>
entries()
The entries() method creates a new iterator object of an array, holding the key/value
pairs for every value in the array. A key represents the index number carrying an item as
its value. It does not affect the original array.
<script>
var a = ['BCA','MCA','B.Tech','M.Tech','Ph.D'];
var newarr = a.entries();
for(var x of newarr)
{
document.write(x +"</br>");
}
</script>
array fill()
array fill() method fills the elements of the given array with the specified static values.
This method modifies the original array.
<script> <script>
var a = ["red", "blue", "yellow"]; var a = ["red", "blue", "yellow"];
var result = a.fill(“Orange"); var result = a.fill(“Orange“, 1);
document.write(a); document.write(a);
</script> </script>
find()
The JavaScript array find() method returns the first element of the given array that
satisfies the provided function condition.
<script>
var a = [5,22,19,25,34];
var result=a.find (x=>x>20);
document.write(result)
</script>
indexOf()
The JavaScript array indexOf() method is used to search the position of a particular
element in a given array.
<script>
var a = ["red", "blue", "yellow", "green"];
var result= a.indexOf("blue");
document.write(result);
</script>
<html> Passing an Array to a function
<head>
<script>
function fn1(num)
{
var x = num.length;
document.write(num);
}
</script>
</head>
<body>
<script>
var Mynum = [1, 2, 2, 4, 5, 4, 7, 8, 7, 3, 6];
var result = fn1(Mynum);
</script>
</body>
</html>