JavaScript DOM
Posted by Superadmin on May 03 2023 14:29:37

JavaScript DOM

By Priya PedamkarPriya Pedamkar
  

JavaScript DOM

Introduction to JavaScript DOM

What is JavaScript DOM?

Hierarchical Structure

As we discussed DOM which is based on the hierarchical structure which includes various types of objects we will see them one by one in detail as follows:

Hierarchical Structure

Examples of JavaScript Document Object Model

Below are the examples of JavaScript DOM:

Example #1

Code:     

<html>
<head>
<title>JavaScript DOM</title>
</head>
<body>
<h4>JavaScript DOM for querySelectorAll</h4>
<p>It's better to be real than to be perfect</p>
<p>Family isn't about blood. It is about who is willing to hold your hand when you need it the most</p>
<div>The Adventure of life is to learn. <br>
The purpose of life is to grow<br>
The nature of life is to change.  <br>
The challenge of life is to overcome. <br>
The essence of life to care<br>
The opportunity of like is to serve <br>
The secret of life is to dare.<br>
The spice of life is to be friend<br>
The beauty of life is to give.
</div>
<p>Your focus determines your reality. Over thinking will destroy your mood.
<br> Breath and let go</p>
<div> This is not just another day, this is another chance to make your dreams come true.</div>
<script>
var paragraphs = document.querySelectorAll('p');
for(var p of paragraphs)
p.style.color = 'gold';
</script>
</body>
</html>

Output:

JavaScript DOM Example 1

Example #2

Code:

<html>
<head>
<title>JavaScript DOM</title>
</head>
<body>
<h4>JavaScript DOM for addEventListener</h4>
<p>This is example which demonstrate how to deal with button for adding event
on button click.</p>
<button>Click Here</button>
<script>
var btnclick = document.querySelector('button');
btnclick.addEventListener('click',check);
function check() {
alert('Together we will drive this CORONA. Be responsible citizen of this great country. Our government, forces, health workers are doing every possible thing to save us from Corona. Stand for yourself, Stand for Nation, Stand for your own safety');
}
</script>
</body>
</html>

Output:

JavaScript DOM Example 2

After clicking the button, the output will be:

 Example 2

Example #3

Code:

<html>
<head>
<title>JavaScript DOM</title>
</head>
<body>
<h4>JAVASCRIPT DOM to replace child element which is belonging to its parent element. In this example it is placed by tag enclosed em tag</h4>
<div>
<strong>Every Teacher once was a Student.<br>
Every Winner once was a Loser<br>
Every expert once was a beginner<br>
But all of them crossing the Bride called LEARNING </strong>
</div>
<script>
var em = document.createElement('em');
var strong = document.querySelector('strong');
var div = document.querySelector('div');
em.textContent = 'Never leave people for small mistakes, Nobody is perfect, Nobody is correct, At the end, Affection is always greater than perfection';
div.replaceChild(em, strong);
</script>
</body>
</html>

Output:

JavaScript DOM Example 3

Conclusion

From all the above details, we came to know that, DOM is a programming interface that works among the HTML document and JavaScript. It is responsible to change HTML elements, HTML attributes, CSS styles, to remove existing HTML elements and attributes, to react on HTML element or to create new event. It is based on a hierarchical structure that includes Window object, Document object, Form object, Form control elements.