The object model, JavaScript gets all the power it needs to create dynamic HTML:
✓ JavaScript can change all the HTML elements in the page
✓ JavaScript can change all the HTML attributes in the page
✓ JavaScript can change all the CSS styles in the page
✓ JavaScript can remove existing HTML elements and attributes
✓ JavaScript can add new HTML elements and attributes
✓ JavaScript can react to all existing HTML events in the page
✓ JavaScript can create new HTML events in the page
What is the DOM?
The DOM is a W3C (World Wide Web Consortium) standard.
The DOM defines a standard for accessing documents:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a
document."
The W3C DOM standard is separated into 3 different parts:
Core DOM - standard model for all document types
XML DOM - standard model for XML documents
HTML DOM - standard model for HTML documents
What is the HTML DOM?
The HTML DOM is a standard object model and programming interface for HTML. It defines:
The HTML elements as objects
The properties of all HTML elements
The methods to access all HTML elements
The events for all HTML elements
The DOM Programming Interface
The HTML DOM can be accessed with JavaScript (and with other programming languages).
In the DOM, all HTML elements are defined as objects.
The programming interface is the properties and methods of each object.
A property is a value that you can get or set (like changing the content of an HTML element).
A method is an action you can do (like add or deleting an HTML element).
the content (the innerHTML) of the <p> element with id="demo":
Example
<html>
<body>
<p id="demo"></p>
<div id= “d1”></div>
<script>
[Link]("demo").innerHTML = "Hello World!";
[Link]("d1").innerHTML = “<p> This is new para </p>”;
</script>
</body>
</html>
The getElementById Method
The most common way to access an HTML element is to use the id of the element.
In the example above the getElementById method used id="demo" to find the element.
The innerHTML Property
The easiest way to get the content of an element is by using the innerHTML property.
The innerHTML property is useful for getting or replacing the content of HTML elements.
The HTML DOM Document Object
The document object represents your web page.
If you want to access any element in an HTML page, you always start with accessing the document
object. Below are some examples of how you can use the document object to access and manipulate
HTML.
Method Description
[Link](id) Find an element by element id
[Link](name) Find elements by tag name
[Link](name) Find elements by class name
Example on ById()
Note <p id= “p1”> NOTE </p>
<p id= “p2”></p>
function change()
{
const x=[Link]("p1");
[Link]("p2").innerHTML="My message "+[Link];
alert(x);
}
Finding HTML Elements by Tag Name
This example finds all <p> elements:
<!DOCTYPE html>
<html>
<body>
<p>First paragraph</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
<p id="demo"></p>
<button id="b1" onclick="fun1()">Click Me</button>
<script>
Function fun1(){
const element = [Link]("p");
[Link]("demo").innerHTML = 'The text in first paragraph (index 0) is: ' +
element[0].innerHTML;
}
</script>
</body>
</html>
OR
Note: There is a list item in html.
<script>
const collection = [Link]("li");
[Link]("demo").innerHTML = collection[1].innerHTML;
</script>
Note : Use “*” for All Element
const collection = [Link]("*");
collection variable store all the tag from <html>, <body> <p> like all tags.
Changing HTML Elements
Property Description
[Link] = new html content Change the inner HTML of an element
[Link] = new value Change the attribute value of an HTML element
[Link] = new style Change the style of an HTML element
Method Description
[Link](attribute, value) Change the attribute value of an HTML element
Adding and Deleting Elements
Method Description
[Link](element) Create an HTML element
[Link](element) Remove an HTML element
[Link](element) Add an HTML element
[Link](new, old) Replace an HTML element
[Link](text) Write into the HTML output stream
Adding a new Paragraph
<script>
// Create element:
const para = [Link]("p");
[Link] = "This is a paragraph.";
// Append to body:
[Link](para);
</script>
Adding a New button.
<script>
const btn = [Link]("button");
[Link] = "Hello Button";
[Link](btn);
</script>
Adding Events Handlers
Method
[Link](id).onclick = function(){
code
}
Description :
Adding event handler code to an onclick event.
The querySelector() method returns the first element that matches a CSS selector.
To return all matches (not only the first), use the querySelectorAll() instead.
<script>
[Link]("p").[Link] = "red";
</script>