Skip to content

Instantly share code, notes, and snippets.

@Draketheb4dass
Created December 4, 2019 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Draketheb4dass/0aabbca639d42304797ce9db6a36389a to your computer and use it in GitHub Desktop.
Save Draketheb4dass/0aabbca639d42304797ce9db6a36389a to your computer and use it in GitHub Desktop.
Dom manipulation with getElementsByClassName
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Finding multiple DOM elements by tag or class name</title>
</head>
<body>
<h1 id="heading">All about dogs</h1>
<p>The domestic <span class="animal">dog</span> is known as man's best friend. The <span class="animal">dog</span> was the first domesticated animal and has been widely kept as a working, hunting, and pet companion. According to recent coarse estimates, there are currently between 700 million and one billion <span class="animal">dog</span>s, making them the most abundant predators in the world. <a href="http://en.wikipedia.org/wiki/Dog">Read more on Wikipedia</a>.</p>
<img src="https://www.kasandbox.org/programming-images/animals/dog_sleeping-puppy.png" height="150">
<img src="https://www.kasandbox.org/programming-images/animals/dogs_collies.png" height="150">
<script>
var headingEl = document.getElementById("heading");
headingEl.innerHTML = "All about cats";
var nameEls = document.getElementsByClassName("animal");
console.log(nameEls);
// HTMLCollection
console.log(nameEls[0]);
for (var i = 0; i < nameEls.length; i++) {
nameEls[i].innerHTML = "cat";
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment