0% found this document useful (0 votes)
4 views

DOM

The Document Object Model (DOM) is a structured representation of a web document that allows dynamic interaction and manipulation of web page content using JavaScript. It consists of a tree structure of nodes, including element nodes, text nodes, and comment nodes, which can be accessed and modified through various methods. Understanding the DOM is essential for creating interactive web applications, and it is important to be familiar with HTML, CSS, and JavaScript to effectively work with it.

Uploaded by

ayoo52294
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DOM

The Document Object Model (DOM) is a structured representation of a web document that allows dynamic interaction and manipulation of web page content using JavaScript. It consists of a tree structure of nodes, including element nodes, text nodes, and comment nodes, which can be accessed and modified through various methods. Understanding the DOM is essential for creating interactive web applications, and it is important to be familiar with HTML, CSS, and JavaScript to effectively work with it.

Uploaded by

ayoo52294
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Overview

The DOM is a critical concept in web development, allowing us to interact with


and manipulate web page content dynamically.

What is DOM?
The DOM, or Document Object Model, is a structured representation of a web
document. It serves as an interface that enables scripts (like JavaScript) to interact
with the content, structure, and style of a webpage. In other words, the DOM
provides a way for us to programmatically access and modify the elements that
make up a webpage.

The Document Object Model, usually referred to as the DOM, is an essential part
of making websites interactive. It is an interface that allows a programming
language to manipulate the content, structure, and style of a website. JavaScript is
the client-side scripting language that connects to the DOM in an internet browser.
Almost any time a website performs an action, such as rotating between a
slideshow of images, displaying an error when a user attempts to submit an
incomplete form, or toggling a navigation menu, it is the result of JavaScript
accessing and manipulating the DOM. In this article, we will learn what the DOM
is, how to work with the document object, and the difference between HTML
source code and the DOM.

In order to effectively understand the DOM and how it relates to working with the
web, it is necessary to have an existing knowledge of HTML and CSS. It is also
beneficial to have familiarity with fundamental JavaScript syntax and code
structure.
DOM Tree Structure
Think of the DOM as a tree structure, where each node represents an element,
attribute, or piece of text within the document. This hierarchical arrangement
reflects the nesting of HTML elements on a webpage. By understanding this
structure, we can traverse the tree and manipulate its nodes to achieve various
outcomes.

All items in the DOM are defined as nodes. There are many types of nodes, but
there are three main ones that we work with most often: Element nodes Text nodes
Comment node

When an HTML element is an item in the DOM, it is referred to as an element


node. Any lone text outside of an element is a text node, and an HTML comment is
a comment node. In addition to these three node types, the document itself is a
document node, which is the root of all other nodes. The DOM consists of a tree
structure of nested nodes, which is often referred to as the DOM tree. You may be
familiar with an ancestral family tree, which consists of parents, children, and
siblings. The nodes in the DOM are also referred to as parents, children, and
siblings, depending on their relation to other nodes.

Accessing DOM Element


To interact with elements, we need to know how to select them.

In order to be proficient at accessing elements in the DOM, it is necessary to have


a working knowledge of CSS selectors, syntax and terminology as well as an
understanding of HTML elements.

The DOM offers methods like getElementById, getElementsByClassName,


getElementsByTagName, and querySelector to locate elements based on their
identifiers, classes, tags, or CSS selectors.
Let's look at some examples:
const h = document.getElementById('header');

consts b = document.getElementsByClassName('button');

consths p= document.getElementsByTagName('p');

const n= document.querySelector('.new’);

Modifying DOM Elements


Once we've selected elements, we can modify their content, attributes, and styles.

We use properties like innerHTML and textContent to change the content of an


element. Attributes can be modified using methods like setAttribute. Styles can be
adjusted using the style property, where we can directly manipulate CSS
properties.
header.innerHTML = 'Welcome to our website!';

firstInput.style.border = '2px solid red';

Creating New DOM Elements


We're not limited to just modifying existing elements. We can also create new
ones. The document.createElement method allows us to create new element nodes.
Once created, we can append them to existing elements using methods like
appendChild and insertBefore.
const newDiv = document.createElement('div');

newDiv.textContent = 'This is a new div!';

document.body.appendChild(newDiv);
Event Handling
Events drive interactivity in web applications, and the DOM allows us to respond
to these events. An event in JavaScript is an action the user has taken. When the
user hovers their mouse over an element, or clicks on an element, or presses a
specific key on the keyboard, these are all types of events. We attach event
listeners using the addEventListener method, specifying the event type and the
function to run when the event occurs.
const button = document.getElementById('myButton');

button.addEventListener('click', function() {

alert('Button clicked!');

});

Manipulating Classes
CSS classes play a crucial role in styling and manipulation. We can add, remove,
and toggle classes using the classList property. This property offers methods like
add, remove, and toggle.
const element = document.getElementById('myElement');

element.classList.add('active');

element.classList.remove('inactive');

element.classList.toggle('highlight');

Traversing the DOM


Traversing the DOM means moving through the tree structure. We can navigate
between parent and child nodes using methods like parentNode and childNodes.
Also, we can access siblings using nextSibling and previousSibling.
const listItem = document.querySelector('li');

const parentList = listItem.parentNode;

const siblings = listItem.previousSibling;


Browser Compatibility
Remember that different browsers might interpret the DOM slightly differently. It's
important to test your code in various browsers to ensure cross-compatibility.

Best Practices
To keep your code organized, separate JavaScript from HTML using external
scripts. Use modern practices like query selectors for more flexible element
selection. The DOM is the backbone of dynamic web development, allowing us to
create interactive and responsive websites.

Remember to practice and experiment to solidify your understanding of these


concepts.

You might also like