[jquery] addClass - can add multiple classes on same div?

jQuery add multiple class

This is my current code, I know its wrong code

$('.page-address-edit').addClass('test1').addClass('test2');

This question is related to jquery

The answer is


You can add multiple classes by separating classes names by spaces

$('.page-address-edit').addClass('test1 test2 test3');

You code is ok only except that you can't add same class test1.

$('.page-address-edit').addClass('test1').addClass('test2'); //this will add test1 and test2

And you could also do

$('.page-address-edit').addClass('test1 test2');

You can do

$('.page-address-edit').addClass('test1 test2');

More here:

More than one class may be added at a time, separated by a space, to the set of matched elements, like so:

$("p").addClass("myClass yourClass");