Explain DOM with example
Solution
The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document and allows programs and scripts to manipulate the content, structure, and style of the document.
Here's a simple example of how the DOM works:
Let's say we have an HTML document like this:
<!DOCTYPE html>
<html>
<head>
<title>My Title</title>
</head>
<body>
<h1>My Heading</h1>
<p>My paragraph.</p>
</body>
</html>
The DOM represents this document as a tree structure where each tag is an object with properties and methods. For example, the "body" tag is an object, and so are the "h1" and "p" tags.
In JavaScript, you can use the DOM to manipulate these objects. For example, you can change the text of the "h1" tag like this:
document.querySelector('h1').textContent = 'New Heading';
After this JavaScript code is executed, the HTML document will look like this:
<!DOCTYPE html>
<html>
<head>
<title>My Title</title>
</head>
<body>
<h1>New Heading</h1>
<p>My paragraph.</p>
</body>
</html>
As you can see, the DOM allows you to interact with and manipulate web documents in a structured and efficient way.
Similar Questions
What is the HTML DOM?
Explain the hierarchical structure of the DOM and how it represents HTML elements.7. Describe the concept of event handling in JavaScript and provide an example of anevent and its associated event handler.
Which is not contained by DOM level 1?
How are the objects organized in the HTML DOM?
What does the "DOM" stand for in JavaScript?OptionsDocument Object ModelDocument Orientation ModelDocument Object ManipulationData Object Model
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.