Chernogorskiy Fedor Chernogorskiy Fedor - 8 months ago 37
AngularJS Question

Get / Set Class of an element in Angular

i'm currently adding a few design features to my submit-work and I'm limited to use AngularJS. There are a few icons, which have to cast a login form to appear and change the classes of the labels. I've added the ng-click to my icon like this:

<img src="images/customer_icon.png" class="customer" ng-click="processForm($event)">

And here's the js-file. At the moment I've just tried to access the "class" attribute, but here i fall.

var app = angular.module('myapp', []);
app.controller('mycontroller', function($scope) {
$scope.processForm = function(obj) {
var elem = angular.element(obj);
alert("Current class value: " + elem.getAttribute("class"));

I'm getting the "elem.getAttribute is not a function" error. Trying to switch "class" to "ng-class", i faced another problem: "ng-class" is not recognised by my css files. Is there any way I can get/set the "class" attribute directly? Guess I have some misunderstanding about the whole Angular model...


Try following snippet.

var app = angular.module('myapp', []);
app.controller('mycontroller', function($scope) {
    $scope.processForm = function(event) {
      var element = angular.element(;
      //Old Class
      console.log("OLD CLASS : " + element.attr('class'));
      //New Class
      console.log("NEW CLASS : " + element.attr('class'));
<script src=""></script>
<div ng-app="myapp" ng-controller="mycontroller">
<img src="" class="customer" ng-click="processForm($event)">

More information angular.element