I've been trying to figure out how to apply the active
class to my unordered list using jQuery. Despite attempting various solutions found on Stackoverflow, nothing seems to be working. My goal is to have the text color change to red when the active
class is added. Can anyone shed some light on why this code isn't functioning as expected?
$("li").click(function() {
$("li").removeClass("active");
$(this).addClass("active");
});
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="navbar">
<li class="active"><a href="#">link</a></li>
<li class=""><a href="#">link</a></li>
<li class=""><a href="#">link</a></li>
<li class=""><a href="#">link</a></li>
<li class=""><a href="#">link</a></li>
<li class=""><a href="#">link</a></li>
</ul>
Additionally, I am encountering an error in my browser console:
error: $("li").click() is not a valid function