Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
jQuery toggleclass simplifies the JavaScript programming language which is easy to learn and we can extend it by writing plugins. It supports many features like animations, event handling, DOM manipulation, lightweight, AJAX support, and cross-browser support.
By using the toggleClass() method we can toggle between the classes with selected elements from the set of matched elements. When we want to switch between the effects or styles whenever we are changes the states of our user interface to make it a more attractive and interactive user interface this can be done quickly and easily by using the toggleClass() method.
Syntax:
In simple words, we can say that the toggleClass() method is used to add or remove a class from the element. The toggleClass() method syntax in the jQuery. It is an inbuilt method in jQuery.
$(selector).toggleClass(className [ , function] [, switch][,options])
It contains some parameters. Some of the details of the parameters are:
Examples of jQuery toggleClass()
This is a simple example of the toggleClass() method. In this example, we can observe the toggleClass() method effect on the paragraph.
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“#btn”).click(function(){
$(“p”).toggleClass(“highlight”);
</script>
<style>
.highlight {
background:blue;
font-size: 200%;
color: white;
}
</style>
</head>
<body>
<button id = “btn”> Click on the button to see toggleClass effect</button>
<p>Example for the toggleClass effect</p>
</body>
</html>
Example #2
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“button”).click(function(){
$(“li”).toggleClass(function(n){
return “listitem_” + n;
</script>
<style>
p {
margin: 8px;
font-size: 20px;
font-weight: bolder;
cursor: pointer;
}
.main {
background: yellow;
}
.listitem_1, .listitem_3,.listitem_5 {
color: blue;
}
.listitem_0, .listitem_2 ,.listitem_4{
color: pink;
}
</style>
</head>
<body>
<ul>
<h1>List of student names</h1>
<li><p class=”main”>Sai</p></li>
<li><p class=”main”>Bharat</p></li>
<li><p class=”main”>Vanitha</p></li>
<li><p class=”main”>Sony</p></li>
<li><p class=”main”>Anna Purna</p></li>
<li><p class=”main”>Vasavi</p></li>
</ul>
<script>
$( “p” ).click(function() {
$( this ).toggleClass( “main” );
});
</script>
<button>Click on the button for toggleClass effects</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“#add”).click(function(){
$(“p”).toggleClass(“highlight”,true);
});
$(“#remove”).click(function(){
$(“p”).toggleClass(“highlight”,false);
</script>
<style>
.highlight {
background-color: blue;
font-size: 200%;
color: white;
}
</style>
</head>
<body>
<p>ToggleClass effects explained by using example.</p>
<p class=”highlight”>Switch parameter to add or remove a class name.</p>
<button id=”add”>Add toggleClass</button>
<button id=”remove”>Remove toggleClass </button>
</body>
</html>
<body>
<!– Code 1–>
<h2> <i> JQuery Modal Demo </i> </h2>
<!– Trigger The JQuery Modal Dialog Box –>
<button id=”modalBtn”>Click to open Modal Demo DialogBox</button>
<!– Our Modal Pop Up Box–>
<div id=”modalDemo” class=”jqModal”>
<!– Our Content for the Modal Dialog Box–>
<div class=”content”>
<span class=”exit”>×</span>
<p>Welcome to JQuery Modal Demo Text…</p>
</div>
</div>
<head>
<style>
body {font-family:Verdana, Geneva, sans-serif;}
/* CSS for The Modal Dialog Box(background) */
.jqModal {
display: none; /* By default, display will be hidden */
position: fixed; /* Position will be fixed */
z-index: 1; /* makes the modal box display on the top */
padding-top: 100px; /* Location of the modal box */
left: 0;
top: 0;
width: 100%; /* width 100%, resizable according to window size */
height: 100%; /* height 100%, resizable according to window size */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* color for the Fallback */
background-color: rgba(0,0,0,0.7); /* Opacity for the fallback */
}
/* CSS for Modal Dialog Content Text */
.content {
background-color:#FFF;
margin: auto;
padding: 20px;
border: 3px solid #809;
width: 80%;
/* CSS for Cross or exit or close Button */
.exit {
color: #809;
float: right;
font-size: 30px;
font-weight: bold;
}
.exit:hover,
.exit:focus {
color: #000;
cursor: pointer;
}
#modalBtn{
background-color: #4CAF50;
color: white;
padding: 12px 20px;
margin: 10px 0;
border: none;
border-radius: 4px;
cursor: pointer;
float: left;
}
</style>
</head>
The last step is the main and important step. It is here where we will trigger the modal pop up and make it visible for the user. We will be attaining the following points through our script:
open the Modal Pop Up on command or when triggered.
To close or exit the Modal Pop Up when the user clicks the (x) button.
To position the Modal Pop Up in the center and stay centered even if the user changes or resizes his viewport.
Script Code:
<script>
// Set object for the modal
var myModal = document.getElementById(“modalDemo”);
// Set object for the button that will trigger the modal box
var myButton = document.getElementById(“modalBtn”);
// Set an element that will close the modal box
var exitBtn = document.getElementsByClassName(“exit”)[0];
// Allows display of modal box, when user clicks the button
myButton.onclick = function() {
myModal.style.display = “block”;
}
// Allows the user to close the modal box, when user will click on (x) button
exitBtn.onclick = function() {
myModal.style.display = “none”;
}
// Allows the user to close the modal box, even when the user clicks anywhere outside of the modal box
window.onclick = function(event) {
if (event.target == myModal) {
myModal.style.display = “none”;
}
}
</script>
Learn more about programming at mcp
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.