cod;nncode. learn. thrive.

Accessing and Modifying DOM Elements In Javascript 🚪

Accessing and Modifying DOM Elements In Javascript

When building dynamic web pages, it's crucial to understand how to access and manipulate the Document Object Model (DOM) in JavaScript. The DOM is a representation of a web page's structure and content, and it provides a way for JavaScript to interact with and modify a page's elements.

In this article, we'll cover the basics of accessing and modifying DOM elements in JavaScript, including how to select elements, change their attributes and styles, and handle events.

🎯 Selecting DOM Elements

Before we can modify a DOM element, we need to select it. JavaScript provides several methods for selecting elements based on their tag name, class, ID, or other attributes.


One of the most common ways to select an element is by its ID using the getElementById method. This method returns a single element with the specified ID, or null if no element with that ID exists.

const element = document.getElementById("my-id");


We can also select elements by their class name using the getElementsByClassName method. This method returns a live HTMLCollection of elements that have the specified class name.

const elements = document.getElementsByClassName("my-class");

querySelector and querySelectorAll

The querySelector method allows us to select an element using a CSS selector. It returns the first matching element or null if no element matches the selector.

const element = document.querySelector(".my-class");

We can also use the querySelectorAll method to select all elements that match a CSS selector. This method returns a NodeList of matching elements.

const elements = document.querySelectorAll(".my-class");

🎨 Modifying DOM Elements

Now that we know how to select DOM elements, let's look at how we can modify them.

Changing Element Content

We can change the content of an element by modifying its innerHTML or textContent properties.

const element = document.getElementById("my-id");
element.innerHTML = "<p>New content</p>";
element.textContent = "New text content";

Modifying Element Attributes

We can modify an element's attributes using the setAttribute and getAttribute methods.

const element = document.getElementById("my-id");
element.setAttribute("src", "image.jpg");
const src = element.getAttribute("src");

Changing Element Styles

We can change an element's styles using its style property. We can set individual styles using the style's property name, or we can set multiple styles using the cssText property.

const element = document.getElementById("my-id"); = "red"; = "color: red; background-color: yellow;";

Handling Events

Finally, we can handle DOM events, such as click or keypress events, using event listeners. We can use the addEventListener method to add a listener function to an element for a specific event type.

const element = document.getElementById("my-id");
element.addEventListener("click", function () {

🤝 Conclusion

Accessing and modifying DOM elements in JavaScript is a fundamental skill for building dynamic web pages. With the methods we've covered in this article, you should be able to select, modify, and handle events for any element on a web page.

Remember to always test your code and handle errors gracefully. With practice and patience, you'll become a master of the DOM in no time!

🙋‍♀️ FAQs

What is the DOM in JavaScript?

The Document Object Model (DOM) is a tree-like representation of the structure and content of an HTML document. It provides a way for JavaScript to interact with and modify the elements on a web page.

What is the difference between innerHTML and textContent?

innerHTML sets or gets the HTML content within an element, including any HTML tags. textContent sets or gets the text content within an element, ignoring any HTML tags.

Can I modify multiple elements at once using JavaScript?

Yes, you can use a loop or a forEach method to modify multiple elements at once, as long as you have selected them using one of the methods we covered.

What is an event listener in JavaScript?

An event listener is a function that waits for a specific event to occur on an element, such as a click or keypress event. When the event occurs, the listener function is executed.

What is a CSS selector in JavaScript?

A CSS selector is a string of characters that selects one or more elements on a web page based on their tag name, class, ID, or other attributes. It is used in conjunction with the querySelector and querySelectorAll methods to select elements in JavaScript.

Your feedback is our favorite notification! Share your thoughts about this page and make us smile.