How can I check if body has specific class? This is my case:
You can use the Mozilla classList for this.
The linked page also has a cross-browser solution.
jQuery.hasClass works for me...
non jQuery answer, try if( document.body.className.match('foo') ) { ... }
document.getElementsByTagName("body")[0].className.match(/foo/)
This returns a string of true
or false
when looking for a specific body class using jQuery's .hasClass
:
$("body").hasClass("your-class-name").toString();
function hasClass(ele,cls) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
if(hasClass(document.getElementById("test"), "test")){//do something};
maybe this helps you :-)
With the use of jQuery it would be easier and less code but nevermind !
This returns a string of true or false
when looking for a specific body class using jQuery's .hasClass
:
if ($("body").hasClass("modal-open")) {
$("body").removeClass("modal-open");
}