To access HTML elements using JavaScript, you can use their attributes like “id” and “class”. Here is an example:
HTML:
<div id="myDiv">This is a div element</div>
JavaScript:
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.
+ There are no comments
Add yours