0

I have html like

 <ul>
    <li>one</li>
    <li>two</li>
    <li>tree</li>
 </ul>

now on ng-click of every 'li' element I want to add class active and remove from other 'li' elements. Also, if the same 'li' is clicked again then I want to remove class 'active'

1
  • From what does the list is created? is it static? is it created using ngRepeat? Commented Jan 22, 2017 at 6:59

1 Answer 1

2

change your html like

  <ul>
    <li ng-click='makeActive("one")' ng-class="{ 'active': active=='one'}">One</li>
    <li ng-click='makeActive("two")' ng-class="{ 'active': active=='two'}">Two</li>
    <li ng-click='makeActive("three")' ng-class="{ 'active': active=='three'}">Three</li>
  </ul>

and add new function

$scope.makeActive = function(item) {
   $scope.active = $scope.active == item?'':item;
}

chekout the working fiddle

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

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.