1

There is HTML code:

<div>
  <span>
    <input ng-click="addClass()" type="checkbox"> 
  </span>
</div>

How I can add CSS class to parent element div after click at input?

I tried:

ng-click="addClass($event)"

Angular JS:

$scope.addClass = function (event){
   event.parent('div').addClass('active');
}

1 Answer 1

3

You could ng-class in your case, maintain a scope variable which will used in ng-class expression, there is no need to use jQuery while you have directive like ng-class which makes your life easy.

Markup

<div ng-class="{active: active}">
  <span>
    <input ng-click="active!= active" type="checkbox"> 
  </span>
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

I use HTML template in ng-view here this code does not work. I get error: TypeError: g.setClass is not a function
In console I see how is changed class without value
@Hamed you can not see the value changed in console..please come here chat.stackoverflow.com/rooms/76764/… I'll guid what exactly needs to do..
I did like as: <input type="checkbox" ng-model="formData.major"> <label class="checkbox-label" ng-class="{active: formData.major}"></label>
@Hamed could you please come on chat

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.