Agyan
adda
Run
Reset
Copy
jQuery Tutorial › jQuery CSS Classes › Active menu and dark mode
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <style> body { transition: background .3s, color .3s; font-family: Arial, sans-serif; } body.dark { background: #1e1e1e; color: #eee; } .menu a { display:inline-block; padding:8px 14px; text-decoration:none; color:inherit; border-radius:6px; } .menu a.active { background:#1f386b; color:#fff; } </style> <nav class="menu"> <a href="#" class="active">Home</a> <a href="#">Tutorials</a> <a href="#">Exams</a> </nav> <p><button id="theme">🌙 Dark mode</button></p> <script> $(function () { $('.menu a').click(function (e) { e.preventDefault(); $('.menu a').removeClass('active'); $(this).addClass('active'); }); $('#theme').click(function () { $('body').toggleClass('dark'); $(this).text($('body').hasClass('dark') ? '☀️ Light mode' : '🌙 Dark mode'); }); }); </script>
Result