I'm trying to add class to 'a' tag on click on 'li' tag. It's my first time using jQuery so probably I'm missing something
$('.nav-item').click( function() {
$(".nav-item a").removeClass("active");
$(".nav-item a").addClass("active");
} );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link" data-target="#myCarousel" data-slide-to="0" href="#">Left</a>
</li>
<li class="nav-item" data-target="#myCarousel" data-slide-to="1">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item" data-target="#myCarousel" data-slide-to="2">
<a class="nav-link" href="#" >Right</a>
</li>
</ul>
$(".nav-item a")selects all links, so you're removing the class from all, then adding it to all.$(".nav-item a")yield different elements?