Open In App

AngularJS angular.element() Function

Last Updated : 12 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The angular.element() Function in AngularJS is used to initialize DOM element or HTML string as an jQuery element. If jQuery is available angular.element can be either used as an alias for the jQuery function or it can be used as a function to wrap the element or string in Angular’s jqlite

Syntax:

angular.element(element)

Parameter value:

  • element: It refers to the HTML DOM element or the string to be wrapped into jQuery.

Example 1: This example illustrates the angular.element() Function in AngularJS.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
    </script>
    <title>angular.element()</title>
</head>
 
<body ng-app="app">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h3>angular.element()</h3>
    <div ng-controller="geek">
        <div ng-mouseenter="style()" id="ide" ng-mouseleave="remove()">
            {{name}}
        </div>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope', '$document',
            function ($scope, $document) {
                $scope.name = "GeeksforGeeks";
 
                $scope.style = function () {
                    angular.element(
                        $document[0].querySelector('#ide')).css({
                            'color': 'white',
                            'background-color': 'green',
                            'width': '200px'
                        });
                };
                $scope.remove = function () {
                    angular.element(
                        $document[0].querySelector('#ide')).css({
                            'color': 'black',
                            'background-color': 'white'
                        });
                };
            }
        ]);
    </script>
</body>
</html>


Output:

 

Example 2: This is another example that illustrates the implementation of angular.element() Function in AngularJS.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
    </script>
    <title>angular.element()</title>
</head>
 
<body ng-app="app" style="text-align:Center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h3> angular.element() </h3>
    <div ng-controller="geek">
        <input type="text" id="text" ng-model="myVal" />
        <button ng-click="getVal()">
            Click me!
        </button>
        <br />
        <b>You typed:</b> {{value}}
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope',
            '$document', function ($scope, $document) {
                $scope.myVal = "";
                $scope.getVal = function () {
                    $scope.value = angular.element(
                        $document[0].querySelector(
                            '#text')).val();
                }
            }]);
    </script>
</body>
</html>


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads