1. Home
  2. Javascript
  3. Has class helper function

Use this helper function to check if the element has a given class. It uses a regular expression to match the queried class name. First attribute of the helper function takes the element name. Pass the name of the class you want to check as the second attribute.

function hasClass(element, class) {
    return !!element.className.match(new RegExp("(\\s|^)" + class + "(\\s|$)"));
Full Javascript cheatsheet
Sitemap Git repository