0

Good day! I have a span in my code. The class contains image that is displayed via span. Once the user clicked the image, its class should change and when another span is clicked, it should go back to the original class

 <span alt="Mode1" ng-class="{'mode': isActive}" ng-click="activeButton()"  ></span></span>

on my controller

  $scope.isActive = false;
  $scope.activeButton = function() {
    $scope.isActive = !$scope.isActive;
  }  

It is already working but my problem is on the load there is nothing displayed and it only shows when I click it. Im using angularjs btw

8
  • What do you mean by nothing is displayed There is only one span and if it is not there where do you click. Explain your problem in detail and post the relevant code in the post. Commented Apr 1, 2016 at 5:55
  • Where is the class that you have to show on load? If mode class is shown on ng-click? Commented Apr 1, 2016 at 5:58
  • @JenishRabadiya the span shows images when it is click the colored image counterpart should be shown but my code above displays nothing only after I clicked it, the colored counterpart is shown Commented Apr 1, 2016 at 6:02
  • @mJunaidSalaat, I think thats my problem because on load, the class that is supposed to be shown is none, How will I put that? Commented Apr 1, 2016 at 6:03
  • ng-class="{'mode': isActive,'in-active-class':!isActive}" Commented Apr 1, 2016 at 6:06

1 Answer 1

0

If you have a toggle on ng-click then just change the ng-class by this.

ng-class="{'mode': isActive, 'classThatIsSupposedToBeShownOnInActive': !isActive}" 

Then it should display the class you want on load and show mode class on ng-click

Hope it helps.

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

6 Comments

Thanks It worked! but how about when I click a different span? the class should go back to the original state
You just have to toggle the $scope.isActive boolean to false on another span click. :)
Try this <span ng-click="anotherSpanClick()" id="differentSpan">Different Span Content</span> $scope.anotherSpanClick = function(){$scope.isActive = false;}
hi again @mJunaidSalaat can I ask again for your help?
I have another problem that seems to be the same as this one but my problem now is how can I change the class of a certain button upon selecting a certain radiobutton? @mJunaidSalaat
|

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.