How to Use HTML Attributes to Access JavaScript Elements?

Estimated read time 1 min read

To access HTML elements using JavaScript, you can use their attributes like “id” and “class”. Here is an example:


<div id="myDiv">This is a div element</div>


const divElement = document.getElementById("myDiv");
console.log(divElement.innerHTML); // Output: "This is a div element"

In this example, the “id” attribute of the div element is used to access it using the document.getElementById() method. Similarly, you can access elements with a specific class using the document.getElementsByClassName() method.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply