-1

I want to add a class to the i tag after clicking of button. how can I achieve this using angular js.

<button type="button" title="Like" ng-click="countLikes(product.title)" class="btn btn-compare">
   <i class="fa fa-thumbs-o-up"></i>
</button>


 $scope.countLikes = function (e) {
      console.log(e);
  }

I wanted output like this. Added exampleClass to the i tag

    <button type="button" title="Like" ng-click="countLikes(product.title)" class="btn btn-compare">
   <i class="fa fa-thumbs-o-up exampleClass"></i>
</button>
2
  • stackoverflow.com/questions/14643836/… Commented Jan 9, 2015 at 7:23
  • No problem, btw I did not add any commet to link, because explanation in that topic seems to be nice and clean :) Commented Jan 9, 2015 at 7:26

3 Answers 3

1

use ng-class directive

<i class="fa fa-thumbs-o-up" ng-class="{'exampleClass' : buttonClicked}"></i>

$scope.countLikes = function (e) {
      $scope.buttonClicked = true;
      console.log(e);
}

when buttonClicked is true i element will get the exampleClass css class

Sign up to request clarification or add additional context in comments.

Comments

0

<button type="button" title="Like" ng-click="countLikes(product.title)" class="btn btn-compare">
   <i class="fa fa-thumbs-o-up" ng-class="{'exampleClass': isClick}"></i>
</button>

$scope.isClick = false;
$scope.countLikes = function (e) {
    $scope.isClick = true;
}

Comments

0

Here is my answer

<button type="button" title="Like"  class="btn btn-compare" ng-click="(myLike = myLike === 'fa-thumbs-o-up' ? 'fa-thumbs-up' : 'fa-thumbs-o-up'); countLikes(product.title)";>
    <i class="fa" ng-class="myLike"></i>
</button>

 $scope.myLike = "fa-thumbs-o-up"; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.