DekGenius.com
JAVASCRIPT
jquery hasclass
if ($( "#foo" ).hasClass('className')) {
$( "#foo" ).removeClass( 'className');
} else {
$( "#foo" ).addClass( 'className');
}
jquery if class exists
// 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 a class exists jquery
if ($(".mydivclass").length){
// Do something if class exists
} else {
// Do something if class does not exist
}
detect if an element has a class jQurey
$("#EL_ID").hasClass("CLASS_NAME");
jquery check if a class exists
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}
jquery hasclass
$( "#mydiv" ).hasClass( "foo" )
jquery check if has class
if ($("#about").hasClass("opened")) {
$("#about").animate({right: "-700px"}, 2000);
}
jquery check if class exists
// 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
}
jquery check if element has class starting with
/* 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 check if class contains any element
jQuery(function ($) {
if ($(".className").length == 0) {
$(".element").removeAttr("style").hide();
}else{
$(".element").removeAttr("style").show();
}
});
jquery hasclass
jQuery(Selector).hasClass('class_name');
if element has class jquery
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}
jquery has class
let hasClass = $('button').hasClass('buttonClass')
console.log(hasClass)
has class in jquery
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hasClass demo</title>
<style>
p {
margin: 8px;
font-size: 16px;
}
.selected {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<p>This paragraph is black and is the first paragraph.</p>
<p class="selected">This paragraph is red and is the second paragraph.</p>
<div id="result1">First paragraph has selected class: </div>
<div id="result2">Second paragraph has selected class: </div>
<div id="result3">At least one paragraph has selected class: </div>
<script>
$( "#result1" ).append( $( "p" ).first().hasClass( "selected" ).toString() );
$( "#result2" ).append( $( "p" ).last().hasClass( "selected" ).toString() );
$( "#result3" ).append( $( "p" ).hasClass( "selected" ).toString() ) ;
</script>
</body>
</html>
© 2022 Copyright:
DekGenius.com