How to select an element by its class name in AngularJS ?

Given an HTML document and the task is to select an element by its className using AngularJS.

Approach: The approach is to use the document.querySelector() method to get the element of a particular className. In the first example the element of className class1 is selected and its background color is changed to green. In the second example, 2 elements of same class are selected and some of the CSS is changed.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.getClass = function () {
                var el = angular.element(
                    document.querySelector(".class1"));
                el.css('background', 'green');
            };
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to select an element by
        its class in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            <p class="class1">
                This is GeeksForGeeks
            </p>
            <input type="button" 
                value="click here" 
                ng-click="getClass()">
        </div>
    </div>
</body>
  
</html>     

chevron_right


Output:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.getClass = function () {
                var el = angular.element(
                    document.querySelectorAll(".class1"));
  
                el.css({ 
                    'background': 'green', 
                    'color': 'white' 
                });
            };
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        How to select an element by 
        its class in AngularJS
    </p>
    <div ng-app="app">
        <div ng-controller="controller">
            <p class="class1">
                This is GeeksForGeeks
            </p>
            <p class="class1">
                A computer science portal
            </p>
            <input type="button" 
                value="click here" 
                ng-click="getClass()">
        </div>
    </div>
</body>
  
</html>    

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.