Horje
How to Use of The class Attribute in JavaScript

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:


Full Example of

Click on a button to hide all elements with the class name "city":
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Use of The class Attribute in JavaScript</h2>
<p>Click the button to hide all elements with class name "city":</p>

<button onclick="myFunction()">Hide elements</button>

<h2 class="city">London</h2>
<p>London is the capital of England.</p>

<h2 class="city">Paris</h2>
<p>Paris is the capital of France.</p>

<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</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>

Output should be:

Full Example of

Don't worry if you don't understand the code in the example above.
Reffered: https://www.w3schools.com/html/html_classes.asp





Related Articles
What is HTML class Attribute HTML class Attribute
How to Use The class Attribute HTML class Attribute
How to use The Syntax For Class HTML class Attribute
How to create HTML Multiple Classes HTML class Attribute
How to create Different Elements Can Share Same Class HTML class Attribute
How to Use of The class Attribute in JavaScript HTML class Attribute

Single Articles
Full Example of HTML class Attribute

Read Full:
HTML class Attribute
Category:
Web Tutorial
Sub Category:
HTML class Attribute
Uploaded:
1 year ago
Uploaded by:
Admin
Views:
154



Share on: