Showing posts with label Class Attribute. Show all posts
Showing posts with label Class Attribute. Show all posts

Tuesday, December 25, 2018

Class Attribute In JavaScript

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.

Another example of class Attribute


Another example of class Attribute


Using tree div elements that point to the same class name.

<!DOCTYPE html>
<html>
<head>
<style>
.cities {
background-color: black;
color: white;
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<div class="cities">
<h2>Delhi</h2>
<p> Delhi is the captical of India</p>
</div>

<div class="cities">
<h2>Kathmandu</h2>
<p> Kathmandu is the captical of Nepal</p>
</div>

<div class="cities">
<h2>Dhaka</h2>
<p> Dhaka is the captical of Bangladesh</p>
</div>

</body>
</html>
 Result:

Delhi

Delhi is the captical of India

Kathmandu

Kathmandu is the captical of Nepal

Dhaka

Dhaka is the captical of Bangladesh

Class Attribute In HTML

Class Attribute In HTML

The class is an attribute which specifies one or more class names for an HTML element. The class name can be used by CSS and JavaScript to do some tasks for HTML elements.

A class attribute can be define within <style> tag or separate file using (.) character.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  .headings {
  color: Tomato;
  font-family: Verdana;
  background-color: black;
}
</style>
</head>
<body>

<h1 class="headings">This is first Heading</h1>
<h2 class="headings">This is first Heading</h2>
<h3 class="headings">This is first Heading</h3>
<h4 class="headings">This is first Heading</h4>

</body>
</html>

Result:

This is first Heading
This is first Heading
This is first Heading

This is first Heading

Tags:

Class Attribute In HTML, HTML