Xarcell
Xarcell

Reputation: 2011

Multiple Selectors With hasClass() Issues

I'm am trying to write some jQuery that has multiple selectors & multiple hasClass.

Basically, there are multiple elements that could have various class names. Any combination thereof, should append the google font.

if ($('nav.main-navigation, header.site-header, main.site-content, footer.site-footer').hasClass('header-menu-font-shojumaru', 'header-title-font-shojumaru', 'header-tagline-font-shojumaru', 'body-font-shojumaru', 'body-heading-font-shojumaru', 'body-widget-title-font-shojumaru', 'footer-font-shojumaru')) {
    $('head').append('<link href="https://fonts.googleapis.com/css?family=Shojumaru" rel="stylesheet">');
}

Unfortunately, this isn't working like I thought it would. It working seems to appear random, although I know it's not. I cannot figure out what the problem is and how to correct it.

Upvotes: 0

Views: 49

Answers (1)

Meligy
Meligy

Reputation: 36594

Your specific case can be achieved by is(), and treating them like a selector.

if ($('nav.main-navigation, header.site-header, main.site-content, footer.site-footer').is('.header-menu-font-shojumaru, .header-title-font-shojumaru, .header-tagline-font-shojumaru, .body-font-shojumaru, .body-heading-font-shojumaru, .body-widget-title-font-shojumaru, .footer-font-shojumaru')) {
    $('head').append('<link href="https://fonts.googleapis.com/css?family=Shojumaru" rel="stylesheet">');
}

Official docs for is()

Upvotes: 1

Related Questions