Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Class Manipulation: jQuery vs Vanilla JS</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> .box{width:200px;padding:15px;background:#eee;margin-bottom:10px;} .box.active{background:#04AA6D;color:#fff;} </style> </head> <body> <div id="jqBox" class="box">jQuery box</div> <button id="jqBtn">Toggle with jQuery</button> <br><br> <div id="jsBox" class="box">Vanilla JS box</div> <button id="jsBtn">Toggle with classList</button> <script> // jQuery $("#jqBtn").on("click", function () { $("#jqBox").toggleClass("active"); }); // Vanilla JS document.getElementById("jsBtn").addEventListener("click", function () { document.getElementById("jsBox").classList.toggle("active"); }); </script> </body> </html>
Output