Reputation:
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
Upvotes: 2
Views: 57
Reputation: 5745
For check if an element have your class name, you can do:
$(var).hasClass('class1');
Upvotes: 0
Reputation: 82241
No you don't need to. addClass internally checks whether class exists. if not, then adds the class
Upvotes: 3