How to check without inside a #id has a . class?

Asked

Viewed 11,031 times

1

Has a top #header that adds the class .hide-bar when scroll but when returning to the top, this class.

How to check with jQuery if you are class is present in this id or not? I need this check to disappear/return with certain blocks.

1 answer

5


You can use the method .hasClass() jQuery:

if($('#header').hasClass('hide-bar')){
    // fazer algo caso tenha a classe
}

I usually avoid jQuery whenever possible in simple features like this. To do this with native Javascript can do so:

var elemento = document.getElementById('header');
if(elemento.className.indexOf('hide-bar') != -1){
    // fazer algo caso tenha a classe
}

Or using Javascript in modern browsers (IE9+):

var elemento = document.getElementById('header');
if(elemento.classList.contains('hide-bar')1){
    // fazer algo caso tenha a classe
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.