Class Attribute In JavaScript
JavaScript can access element with a specified className
by using the getElementsByClass() method.
Example:
<!DOCTYPE
html>
<html>
<body>
<h2>Using
The class Attribute in JavaScript</h2>
<p>Click
the button, to hide all elements with the class name "city", with
JavaScript:</p>
<button
onclick="myFunction()">Hide elements</button>
<h2
class="city">Delhi</h2>
<p>Delhi is the capital of India.</p>
<h2
class="city">Kathmandu</h2>
<p>Kathmandu is the capital of Nepal.</p>
<h2
class="city">Dhaka</h2>
<p>Dhaka is the capital of Bangladesh.</p>
<script>
function
myFunction() {
var x =
document.getElementsByClassName("city");
for (var i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
}
</script>
</body>
</html>
|
Result:
Using The class Attribute
in JavaScript
Click the button, to hide all elements with the class name
"city", with JavaScript:
Hide
elements
Delhi is the capital of India.
Kathmandu is the capital of Nepal.
Dhaka is the capital of Bangladesh.
EmoticonEmoticon