javascript get element by class

Today, We want to share with you javascript get element by class.In this post we will show you jquery get child element by class, hear for javascript hide element by class we will give you demo and example for implement.In this post, we will learn about Jquery Count Child Elements With Class with an example.

Example : How to Get element by class name in AngularJs?

  <script>
var ShopEcomApplication = angular.module("ShopEcomApplication", []);
 ShopEcomApplication.controller("pakainfoController", function($scope) {
$scope.getClassElm = function() {
	 var element = angular.element(document.querySelector(".pakainfoWeb"));
          element.css('color', 'blue');
	 
};
});
</script>

Html Part-

<div ng-app="ShopEcomApplication">  
<div ng-controller="pakainfoController">  
<h3>AngularJs Is Cool!</h3>
<p class="pakainfoWeb">
Welcome to Our Best Blog using Pakainfo.com....
</p>
<input type="button" value="Change Text Color" ng-click="getClassElm()"> 
</div> 

Get element by class name in AngularJs:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var ShopEcomApplication = angular.module("ShopEcomApplication", []);
 ShopEcomApplication.controller("pakainfoController", function($scope) {
$scope.getClassElm = function() {
	 var element = angular.element(document.querySelector(".pakainfoWeb"));
          element.css('color', 'blue');
	 
};
});
</script>
</head>
<body>  
<div ng-app="ShopEcomApplication">  
<div ng-controller="pakainfoController">  
<h3>AngularJs Is Best Client Side Programming Lanaguages!</h3>
<p class="pakainfoWeb">
This is test paragraph....
</p>
<input type="button" value="Update Text Color " ng-click="getClassElm()"> 
</div> 

</body>  
</html>

Example : 2 JavaScript getElementsByClassName

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Select Element by Class in JavaScript - www.pakainfo.com</title>
<style>
    .cart-data{
        height: 50px;
        background: #ddd;
        margin: 20px;
    }
    .cart-data.bordered{
        border: 5px solid #333;
    }
</style>
</head>
<body>
<div class="cart-data"></div>
<div class="cart-data bordered"></div>
<div class="cart-data"></div>
<div class="cart-data bordered"></div>
<div class="cart-data"></div>

<script>
    var cart-dataes = document.getElementsByClassName("cart-data");

    // Select first cart-data and style it with red background
    cart-dataes[0].style.background = "red";

    /* Select elements having both "cart-data" and "bordered" class 
    and style them with yellow background */
    var borderedBoxes = document.getElementsByClassName("cart-data bordered");
    for(var i = 0; i < borderedBoxes.length; i++){
        borderedBoxes[i].style.background = "yellow";
    }
    // Select last element and style it with green background
    cart-dataes[cart-dataes.length - 1].style.background = "green";
</script>
</body>
</html>

Example : 3 HTML DOM getElementsByClassName() Method

var x = document.getElementsByClassName("example").length;

//or

var x = document.getElementsByClassName("example");
var i;
for (i = 0; i < x.length; i++) {
  x[i].style.backgroundColor = "red";
}

//or


var elementsArray = document.getElementsByClassName("someclassname");

//or

 var x = document.getElementsByClassName("example");

 //or

 var elements = document.querySelector(".class");

 //or

 const whiteBoxes = document.getElementsByClassName("cart-data-white")

  //or
//get element by class
  var elements = document.getElementsByClassName(names); // or:
var elements = rootElement.getElementsByClassName(names);

I hope you get an idea about jquery get element by class.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.