I would like to read out the classnames attached to a certain element and take action based on that the classname contains a certain value (“active”). The classname could be something like: class=”bla-bla-bla first active”. What is the best way to do that?
Share
Class names are separated by spaces, so you just need
$(something).hasClass('active')Additionally, you could use the
.is()method, like this:$(somthing).is('.active'), which returnstrueorfalse.Finally, you might be better off using it as the selector, like so:
$('li.active').doSomething()