Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Attributes

0/1

HTML Paragraphs

0/1

HTML Formatting

0/1

HTML Comments

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML Symbols

0/1
Text lesson

Different Elements Can Share Same Class

Various HTML elements can reference the same class name.

In the provided example, both <h2> and <p> elements are associated with the “city” class, thus inheriting the same style.

Example

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

Use of The class Attribute in JavaScript

JavaScript can utilize the class name to execute specific tasks on particular elements.

Elements with a specific class name can be accessed by JavaScript using the getElementsByClassName() method.

Example

Click a button to conceal all elements having the class name “city”.

<script>
function myFunction() {
  var x = document.getElementsByClassName(“city”);
  for (var i = 0; i < x.length; i++) {
    x[i].style.display = “none”;
  }
}
</script>

Summary of the Chapter

  • The HTML class attribute designates one or multiple class names for an element.
  • Classes are utilized by CSS and JavaScript to target and interact with particular elements.
  • The class attribute is applicable to any HTML element and is case sensitive.
  • Diverse HTML elements can reference the same class name.
  • JavaScript employs the getElementsByClassName() method to retrieve elements with a specified class name.

 

Click to Learn