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

1

It has a top #header that adds the class .hide-bar when scrolling but when returning to the top, that class disappears.

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

    
asked by anonymous 13.12.2014 / 14:42

1 answer

4

You can use the .hasClass() method of jQuery like this:

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

I usually avoid jQuery whenever possible in simple functionalities like this. To do this with native JavaScript you can do this:

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
}
    
13.12.2014 / 14:44