xxxxxxxxxx
// 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
}
xxxxxxxxxx
if ($(".mydivclass").length){
// Do something if class exists
} else {
// Do something if class does not exist
}
xxxxxxxxxx
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}
xxxxxxxxxx
if ($("#about").hasClass("opened")) {
$("#about").animate({right: "-700px"}, 2000);
}
xxxxxxxxxx
/* 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
}
xxxxxxxxxx
// 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
}
xxxxxxxxxx
jQuery(function ($) {
if ($(".className").length == 0) {
$(".element").removeAttr("style").hide();
}else{
$(".element").removeAttr("style").show();
}
});
xxxxxxxxxx
if ($('#elementID').hasClass('className')) {
console.log('Class exists');
} else {
console.log('Class does not exist');
}
xxxxxxxxxx
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}