user3390335
user3390335

Reputation:

jQuery addClass method

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

Answers (2)

P. Frank
P. Frank

Reputation: 5745

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

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

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

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

Reference

Upvotes: 3

Related Questions