How to change the button color after clicking it using AngularJS ? Last Updated : 01 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Created an HTML button and the task is to change the background color of the button when pressing it with the help of AngularJS. This task can be accomplished using the ng-click directive that helps to toggle the display of the content when the button or any specific HTML element is clicked. Here, the ng-controller directive is applied to add the controller to the application, which can be utilized to add methods, functions, and variables, which in turn, can be called on some event like click, etc to perform a certain action. Approach: In this approach, we will try to change the class or id of the button, and the CSS of those classes/IDs will change the background color of the button. Example 1: In this example, the class is changed from red to green using the ng-click directive in AngularJS. HTML <!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.myButton = 'red'; $scope.changeCol = function() { $scope.myButton = "green"; }; }); </script> <style type="text/css"> .red { background: red; color: white; } .green { background: green; color: white; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> How to change the color of the button in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <button ng-click="changeCol()" class="{{myButton}}"> Click to change </button> </div> </div> </body> </html> Output: Example 2: In this example, the id of the button is changed from blue to green using the ng-click directive in AngularJS. HTML <!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.ID = 'blue'; $scope.changeCol = function() { $scope.ID = "green"; }; }); </script> <style> #blue { background: blue; color: white; } #green { background: green; color: white; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> How to change the color of the button in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <button ng-click="changeCol()" id="{{ID}}"> Click to change </button> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to change the button color after clicking it using AngularJS ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies HTML AngularJS HTML-Misc AngularJS-Misc +1 More Similar Reads How to change the font of HTML5 Canvas using a button in Angular.js? In this article, we are going to learn about how to change the font of HTML5 Canvas using a button in AngularJS. With the help of a click font's property can change by the user whether it is font-size or font-style. Syntax: For font size (Change the font size accordingly): variable.fontSize = "100px 2 min read How to set checkbox checked on button click in AngularJS? In this article, we will see how to set checkboxes checked with the click of a button in AngularJS. Approach: The approach is to use the ng-checked directive to check the checkbox in the DOM. In the first example, a single checkbox is checked by the button and In the second example, multiple checkbo 2 min read How to disable buttons using AngularJS ? In this article, we will see how to disable the button using the particular directive in AngularJS. Sometimes, we need to disable the button, and link on the click event. This task can be accomplished by implementing the ng-disabled directive that is used to enable or disable HTML elements. Syntax: 2 min read How to change color of the Object value in Angular ? JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. JSON objects are key: value pairs. In this article, we will learn How to change the color of the object value in Angular. Steps for Insta 3 min read How to disable a button depending on a checkboxâs state in AngularJS ? In this article, we will learn to disable the button depending upon the check-box status in Angular. We will use the Angular JS directive named ng-disabled to disable the button by unchecking the box. Please refer to AngularJS ng-disabled Directive. The ng-disabled directive is used to enable or dis 2 min read Like