Vanilla JS - A toggleClass function
How do I make an vanilla js - a toggleclass function?
What is a vanilla js - a toggleclass function? How do you make a vanilla js - a toggleclass function? This script and codes were developed by Joe Harry on 25 October 2022, Tuesday.
Vanilla JS - A toggleClass function - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Vanilla JS - A toggleClass function</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <p>An example function to toggle classes without needing jQuery.</p>
<div id="sqr" class="square">Click me to toggle</div>
<p id="current-classes">squares current classes: </p> <script src="js/index.js"></script>
</body>
</html>
Vanilla JS - A toggleClass function - Script Codes CSS Codes
* { -webkit-transition: 0.2s; transition: 0.2s;
}
body { background-color: #333; color: rgba(255,255,255,0.75); height: 100vh; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; align-items: center; -ms-flex-flow: column; flex-flow: column; text-align: center; font-family: Verdana, Futura, sans-serif;
}
p { max-width: 200px;
}
.square { width: 200px; height: 200px; cursor: pointer; line-height: 200px; box-shadow: 0 0 60px rgba(0,0,0,0.4); border-radius: 5px; background-color: #F5F5F5; color: rgba(0,0,0,0.75);
}
.large { width: 300px; height: 300px; line-height: 300px;
}
.amber { background-color: #FFC107;
}
.indigo { background-color: #3F51B5;
}
Vanilla JS - A toggleClass function - Script Codes JS Codes
//Our function to toggle classes without jQuery, it takes two parameters:
//1. The element to have a class toggled
//2. The name of the class we want to toggle
function toggleClass(el, _class) { //Check if the element exists, //& if it has the class we want to toggle if (el && el.className && el.className.indexOf(_class) >= 0) { //Element has the class, so lets remove it... //Find the class to be removed (including any white space around it) var pattern = new RegExp('\\s*' + _class + '\\s*'); //Replace that search with white space, therefore removing the class el.className = el.className.replace(pattern, ' '); } else if (el){ //Element doesn't have the class, so lets add it... el.className = el.className + ' ' + _class; } else { //Our element doesn't exist console.log("Element not found"); }
}
var square = document.getElementById("sqr"), body = document.body, text = document.getElementById("current-classes");
//decorational.. show current classes
text.innerHTML = "squares current classes: <code>'" + square.className + "'</code>";
//on click toggle some classes
square.addEventListener("click", function(){ toggleClass(body, 'indigo'); toggleClass(square, 'amber'); toggleClass(square, 'large'); //decorational.. show current classes text.innerHTML = "squares current classes: <code>'" + square.className + "'</code>";
});
Developer | Joe Harry |
Username | woodwork |
Uploaded | October 25, 2022 |
Rating | 3 |
Size | 2,532 Kb |
Views | 24,288 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Fake 3D | 2,813 Kb |
Your Planet is Loading... | 4,051 Kb |
Loading Christmas... | 3,086 Kb |
Stronger Together | 3,751 Kb |
Flower Spinner | 2,584 Kb |
Untranslatable | 7,119 Kb |
100 | 4,932 Kb |
Expanding Contact Form | 6,876 Kb |
Typophile | 10,877 Kb |
Video Loading | 2,118 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Two joint circles - One element | Berdejitendra | 1,704 Kb |
Testimonial Fancy tabs responsive | Amit-webdesigner | 3,056 Kb |
SVG Animation | Thepheer | 4,793 Kb |
Bootstrap 3 Price Table | Honglio | 2,655 Kb |
CSS Heart Loaders | Nourabusoud | 2,161 Kb |
Elon Musk - Tribute Page - FreeCodeCamp | Yunnimun | 8,615 Kb |
Pure CSS Read More Arrow | Zephyr | 1,747 Kb |
Apex Calculator | Michaelwnelson | 2,370 Kb |
The CodePen Logo | Kindofone | 4,259 Kb |
Reading Grid | Tappily | 4,306 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!