Answers for "jquery if hasClass"

19

jquery hasclass

if ($( "#foo" ).hasClass('className')) {
	$( "#foo" ).removeClass( 'className');
} else {
  $( "#foo" ).addClass( 'className');
}
Posted by: Guest on July-01-2020
6

detect if an element has a class jQurey

$("#EL_ID").hasClass("CLASS_NAME");
Posted by: Guest on May-14-2020
1

jquery check if a class exists

if ($(".mydivclass")[0]){
    // Do something if class exists
} else {
    // Do something if class does not exist
}
Posted by: Guest on May-06-2020
3

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
}
Posted by: Guest on March-15-2020
0

jQuery check if class contains any element

jQuery(function ($) {
	if ($(".className").length == 0) {
		$(".element").removeAttr("style").hide();
	}else{
		$(".element").removeAttr("style").show();
	}	
});
Posted by: Guest on September-07-2021
0

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>
Posted by: Guest on October-09-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language