// given an element: <div id="myID" class="wrapper active">
//JQuery answer
if ( $("#myID").hasClass("active") ){
//true: myID has class active
} else {
//false: myID does not have class active
}
if ($(".mydivclass").length){
// Do something if class exists
} else {
// Do something if class does not exist
}
$("#EL_ID").hasClass("CLASS_NAME");
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}
if ($("#about").hasClass("opened")) {
$("#about").animate({right: "-700px"}, 2000);
}
// if class exist the expresion will return true and the code from if statement will execute
if ($(".mydivclass")){
// Do something if class exists
} else {
// Do something if class does not exist
}
/* Answer to: "jquery check if element has class starting with" */
if(!$(this).is('[class*="answerbox"]')) {
//Finds element with no answerbox class
} else {
//The element has already an answerbox class
}
jQuery(function ($) {
if ($(".className").length == 0) {
$(".element").removeAttr("style").hide();
}else{
$(".element").removeAttr("style").show();
}
});
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}