jQuery ToggleClass to Switch Between Classes

jQuery ToggleClass to Switch Between Classes

Today, We want to share with you jQuery ToggleClass to Switch Between Classes.
In this post we will show you jQuery ToggleClass to Switch Between Classes, hear for How to use switchClass and toggleClass in jQuery we will give you demo and example for implement.
In this post, we will learn about How to toggle between two classes in jQuery with an example.

The toggleClass() method main used to toggles between adding class and removing class one or more class names to generated from the selected elements.

jQuery ToggleClass Syntax

$(yourselector).toggleClass(yourclassname,function(index,yourcurrentclass),switch)

The jQuery simple toggleCLass() method is used to class add or class remove one or more classes using jquery from the selected elements.and This method toggles between class adding and removing class one or more class name.(Like as up and down or show or hide “switch”)

Read Also:  PHP Multiple Checkbox Array Handling Tutorial Example

The toggleClass( class ) add and remove method adds the specified class if it is not present and used to switch, removes the specified class if it is present.

The jQuery toggle() method is a special type of method which is used to here exmample of toggle between the hide() add class and show() remove class method. It shows the hidden elements and hides the shown element.

Read Also:  Laravel 6 Custom Authentication System

Example of jquery toggle between two classes

HTML

<p id="pruduct1" class="toggle red">Laravel</p>
<p id="pruduct2" class="toggle green">PHP</p>
<p id="pruduct3" class="toggle green">AngularJs</p>
<p id="pruduct4" class="toggle green">Vuejs</p>
<button>Languages Demo</button>

CSS

.red{ color:red; }
.yellow{ color:yellow; }
.blue{ color:blue; }

jQuery

$('button').click(function(){
  $('.toggle').toggleClass('red yellow');
});

jQuery .toggleClass() method used to adds class or removes the class for your selected elements.

$(this).toggleClass("gridView plainView");

or
$(document).ready(function(){
	$(".menu-text").addClass("none");
	$("#mgmenu").addClass("toggled");
	$(".menu-toggle").click(function(e) {
		e.preventDefault();
		$("#mgmenu").toggleClass("toggled");
		$(".menu-toggle i").toggleClass("fa-angle-double-left fa-angle-double-right");
		$(".menu-text").toggleClass("none block");
		$(".menu-area").toggleClass("none block");
	});
	$("#navmenu").click(function(e) {
		e.preventDefault();
		$(".dropdown-toggle").toggleClass("none block");
	});
	$(".mainmenu").click(function () 
	{
	var obj = $(this).next();
	$(obj).slideToggle("slow");
	$(this.parentNode).toggleClass("active");	
    $("i", this).toggleClass("fa-angle-up fa-angle-down");
		
    });
});

View Demo

We hope you get an idea about jQuery ToggleClass to Switch Between Classes
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.