2

When I call the jQuery method .addClass() do I need to check if the class already exists or it's already implemented? I mean, if I call the same method twice, like

$(var).addClass('class1');
$(var).addClass('class1');

does it cause any issue or it's fine? Thanks

1
  • 3
    It's absolutely fine. jQuery is very tolerant. Commented Oct 19, 2015 at 8:01

2 Answers 2

3

No you don't need to. addClass internally checks whether class exists. if not, then adds the class

Reference

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

1 Comment

Thanks for the answer
0

For check if an element have your class name, you can do:

$(var).hasClass('class1');

Comments

Your Answer

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