Say I have this html element
<div id="hello" class="hello option john">Hello</div>
<div id="hello" class="hello john">Hello</div>
. Now I select the element with javascript by it’s Id. How would I do an equivalent of if($('hello').hasClass('option')){//do stuff}(jQuery) except in plain Javascript?
That’ll do it.
EDIT: demo
or as a prototyped function:
and
Update 2015:
In modern browsers including IE 10 you can write:
See the reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
It’s supported in all major browsers (ref: http://caniuse.com/#search=classlist)