I'm having trouble figuring out why the icon won't change no matter what I do.
$( ".inner-switch" ).on("click", function() {
if( $( "body" ).hasClass( "dark" )) {
$( "nav" ).removeClass( "dark-force" );
$( "body" ).removeClass( "dark" );
$(this).find("i").toggleClass("fas fa-moon");
} else {
$( "body" ).addClass( "dark" );
$( "nav" ).addClass( "dark-force" );
$(this).find("i").toggleClass("fas fa-sun");
}
});
.dark {
background-color: #222;
}
.dark-force * {
background-color: #222;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<nav class="navbar">
<div class="hero-body">
<div class="container">
<div class="navbar-brand">
<a href="/">
<img src="{% static 'portfolio/Logo.png' %}" width=60>
</a>
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary is-rounded is-outlined">
<strong>Blog</strong>
</a>
</div>
</div>
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary is-rounded">
<strong>Say Hello!</strong>
</a>
</div>
</div>
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary is-rounded inner-switch">
<i class="fas fa-moon"></i>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</nav>
The dark mode works fine, but the icon remains stuck on the moon (fas fa-moon
). While I may be new to javascript, I have a good understanding of HTML and CSS. Any guidance would be greatly appreciated. Thank you for taking the time to read this! Stay safe and stay positive!