The class name can also be used by JavaScript to perform certain tasks for specific elements.
JavaScript can access elements with a specific class name with the getElementsByClassName() method:
Live Demo & Try it yourself!
Video is not available... Embedded is previous playlist.
The class name can also be used by JavaScript to perform certain tasks for specific elements.
JavaScript can access elements with a specific class name with the getElementsByClassName() method:
<!DOCTYPE html>
<html>
<head>
<title>class in JavaScript</title>
<style>
.school {background-color: tomato;color: white;padding: 10px;}
</style>
</head>
<body>
<h2>Use of The class Attribute in JavaScript</h2>
<p>Click the button to hide all elements with class name "school":</p>
<button onclick="myFunction()">Hide elements</button>
<h2 class="school">GSSS KHOKHAR</h2>
<p>GOVT.SEN.SEC.SCHOOL, KHOKHAR</p>
<h2 class="school">GHS, HARAZ</h2>
<p>GOVT. HIGH SCHOOL, HARAZ</p>
<h2 class="school">GSSS SARAINAGA</h2>
<p>GOVT.SEN.SEC.SCHOOL, SARAINAGA</p>
<script>
function myFunction() {
var x = document.getElementsByClassName("school");
for (var i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
}
</script>
</body>
</html>