Skip to content
Related Articles

Related Articles

Improve Article

How to empty the content of an element using AngularJS ?

  • Last Updated : 15 Oct, 2020

Here the task is to remove the content of an element in HTML DOM with the help of AngularJS.

Approach: First select the element whose content is to be emptied. Then use the empty() method to remove the content of the element.  

Example 1: In this example the content of element of ID(‘div’) is emptied.




<!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.emptyEl = function () {
                var el = angular.element(
                    document.querySelector('#div'));
                el.empty();
            };
        });
    </script>
    <style>
        #div {
            height: 80px;
            width: 170px;
            margin: 0 auto;
            background: green;
            color: white;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        Empty the content of an element
    </p>
    <div id="div">
        GeeksForGeeks
        <br>
        <br>
        A Computer Science portal
    </div>
    <br>
    <div ng-app="app">
        <div ng-controller="controller">
            <input type="button" 
                value="Click here" 
                ng-click="emptyEl()">
        </div>
    </div>
</body>
  
</html> 

Output:



Example 2: In this example the content of an element along with its children are emptied.




<!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.emptyEl = function () {
                    var el = angular.element(
                        document.querySelector('#div'));
                    el.empty();
                };
            });
    </script>
  
    <style>
        #div {
            height: 80px;
            width: 170px;
            margin: 0 auto;
            background: green;
            color: white;
        }
  
        #div2 {
            background: blue;
            margin: 10px;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>
        Empty the content of an element
    </p>
    <div id="div">
        <h4>This is parent element</h4>
        <div id="div2">This is child element</div>
    </div>
    <br>
    <div ng-app="app">
        <div ng-controller="controller">
            <input type="button" 
                value="Click here" 
                ng-click="emptyEl()">
        </div>
    </div>
</body>
  
</html>

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :