The getElementsByClassName of Document for JS returns a HTMLCollection of the elements in the object on which the method was invoked (a document or an element) that have all the classes given by classNames. The classNames argument is interpreted as a space-separated list of classes.
collection = document.getElementsByClassName(classNames)
collection = element.getElementsByClassName(classNames) A space-separated list of classes.
<!doctype html>
<html>
<body>
<button>button</button>
<div id="myid1">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid2">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid3">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<script>
function myfunction()
{
const classNames = "myclass";
const collection = document.getElementsByClassName(classNames);
const item = collection[0];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<div id="myid1">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid2">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid3">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<script>
function myfunction()
{
const classNames = "myclass";
const collection = document.getElementsByClassName(classNames);
const item = collection[collection.length - 1];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<div id="myid1">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid2">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid3">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<script>
function myfunction()
{
const element = document.getElementById("myid2");
const classNames = "myclass";
const collection = element.getElementsByClassName(classNames);
for(let i = 0; i < collection.length; ++i)
{
const item = collection[i];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<div id="myid1">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid2">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid3">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<script>
function myfunction()
{
const element = document.getElementById("myid2");
const classNames = "myclass";
const collection = element.getElementsByClassName(classNames);
const item = collection[0];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<div id="myid1">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid2">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<div id="myid3">
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
<p class="myclass">paragraph</p>
</div>
<script>
function myfunction()
{
const element = document.getElementById("myid2");
const classNames = "myclass";
const collection = element.getElementsByClassName(classNames);
const item = collection[collection.length - 1];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>
<!doctype html>
<html>
<body>
<button>button</button>
<p class="myclass1">paragraph</p>
<p class="myclass2">paragraph</p>
<p class="myclass3">paragraph</p>
<p class="myclass1 myclass2">paragraph</p>
<p class="myclass1 myclass3">paragraph</p>
<p class="myclass2 myclass3">paragraph</p>
<p class="myclass1 myclass2 myclass3">paragraph</p>
<script>
function myfunction()
{
const classNames = "myclass1 myclass2";
const collection = document.getElementsByClassName(classNames);
for(let i = 0; i < collection.length; ++i)
{
const item = collection[i];
item.innerHTML = "getElementsByClassName";
item.style.color = "red";
}
}
document.querySelector("button").addEventListener("mouseover", myfunction);
</script>
</body>
</html>