Before we dive into DOM Manipulation, let's have a quick overview of what the Document Object Model (DOM) is.
The DOM is an API (Application Programming Interface) for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can interact with the page.
Now, let's start with some basic DOM Manipulation concepts.
To manipulate elements on the page, we first need to select them. Here are some of the most common ways to select elements in the DOM:
The getElementById() method is used to select an element by its ID. It returns the element as an object.
const element = document.getElementById("my-id");The getElementsByClassName() method is used to select elements by their class name. It returns an array-like object of all elements that have the specified class name.
const elements = document.getElementsByClassName("my-class");The getElementsByTagName() method is used to select elements by their tag name. It returns an array-like object of all elements that have the specified tag name.
const elements = document.getElementsByTagName("p");The querySelector() method is used to select the first element that matches a specified CSS selector. It returns the element as an object.
const element = document.querySelector("#my-id .my-class");The querySelectorAll() method is used to select all elements that match a specified CSS selector. It returns an array-like object of all elements that match the selector.
const elements = document.querySelectorAll(".my-class");Once we have selected an element, we can modify its properties using various methods.
1. Changing the content of an element
To change the content of an element, we can use the innerHTML property.
const element = document.getElementById("my-id");
element.innerHTML = "New content";2. Changing the attributes of an element
To change the attributes of an element, we can use the setAttribute() method.
const element = document.getElementById("my-id");
element.setAttribute("class", "new-class");3. Changing the style of an element*
To change the style of an element, we can use the style property.
const element = document.getElementById("my-id");
element.style.color = "red";
element.style.backgroundColor = "yellow";We can also create new elements and add them to the page.
1. Creating an element To create a new element, we can use the createElement() method.
const element = document.createElement("div");2. Adding an element to the page
To add a new element to the page, we first need to select an existing element to add it to. We can then use the appendChild() method to add the new element as a child of the selected element.
const parentElement = document.getElementById("my-id");
parentElement.appendChild(element);We can also remove elements from the page.
1. Removing an element
To remove an element from the page, we first need to select the element. We can then use the remove() method to remove it.
const element = document.getElementById("my-id");
element.remove();2. Removing all child elements of an element
To remove all child elements of an element, we can use the innerHTML property to set the content of the element to an empty string.
const parentElement = document.getElementById("my-id");
parentElement.innerHTML = "";Events are actions or occurrences that happen in the browser, such as clicking a button or scrolling the page. We can use JavaScript to handle these events and perform actions in response to them.
To add an event listener to an element, we can use the addEventListener() method. This method takes two arguments: the event to listen for, and the function to call when the event is triggered.
const button = document.getElementById("my-button");
button.addEventListener("click", function () {
// Do something
});To remove an event listener from an element, we can use the removeEventListener() method. This method takes the same arguments as addEventListener().
const button = document.getElementById("my-button");
const clickHandler = function () {
// Do something
};
button.addEventListener("click", clickHandler);
// Later on...
button.removeEventListener("click", clickHandler);Example:
Here is an example that demonstrates some of the concepts we've covered:
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation Example</title>
</head>
<body>
<h1 id="my-heading">Hello, World!</h1>
<button id="my-button">Click me!</button>
<script>
const heading = document.getElementById("my-heading");
const button = document.getElementById("my-button");
button.addEventListener("click", function () {
heading.innerHTML = "Button clicked!";
heading.style.color = "red";
});
</script>
</body>
</html>In this example, we select the heading and button elements using their IDs. We then add an event listener to the button that changes the heading's content and style when the button is clicked.
- document.getElementById(): click
- document.getElementsByClassName(): click
- document.getElementsByTagName(): click
I hope this helps you understand the basics of DOM Manipulation in JavaScript! Let me know if you have any further questions.