Kyle Kyle - 1 year ago 93
Javascript Question

"hasClass" with javascript?

How do you do jQuery's

with plain ol' javascript? For example,

<body class="foo thatClass bar">

What's the javascript way to ask if
has "thatClass"?

Answer Source

You can check whether element.className matches /\bthatClass\b/.
\b matches a word break.

Or, you can use jQuery's own implementation:

var className = " " + selector + " ";
if ( (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(" thatClass ") > -1 ) 

To answer your more general question, you can look at jQuery's source code on github or at the source for hasClass specifically in this source viewer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download