DOM Methods
DOM Methods
<!DOCTYPE html>
<html>
<head>
<title>getElementsByClassName Example</title>
</head>
<body>
<script>
function changeColor() {
// Loop through all the elements and change their color to blue
elements[i].style.color = "blue";
}
</script>
</body>
</html>
Syntax:
document.getElementsByTagName(tagName);
Program:
<!DOCTYPE html>
<html>
<body>
<p>HELLO</p>
<p>For</p>
<p>YOU</p>
<script>
const p = document.getElementsByTagName("p");
document.write(p);
</script>
</body>
</html>
Program 2:
<!DOCTYPE html>
<html>
<head>
<title>getElementsByTagName() Example</title>
</head>
<body>
<h3>List of Fruits</h3>
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
<li>Orange</li>
</ul>
<script>
function changeText() {
</script>
</body>
</html>
listItems is assumed to be an array-like list of HTML elements, such as <li> items in a list.
listItems[i].textContent accesses the textContent property of each list item in listItems, allowing
you to change the text inside that list item.
HTML DOM querySelector(): This method returns the first match of an element that is found
within the HTML document with the specific selector. If there is no match, null is returned.
Syntax:
document.querySelector(selector);
HTML DOM querySelectorAll(): This method returns a static NodeList of all elements that are
found within the HTML document with the specific selector.
Syntax:
document.querySelectorAll(selector);
querySelector(): Selects the first element that matches a specified CSS selector.
querySelectorAll(): Selects all elements that match a specified CSS selector, returning a
NodeList (like an array).
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function changeContent()
</script>
</body>
</html>