Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

383
Views
Active link name is not changing color in bootstrap navbar

This is a single-page website currently I am working on. But When I click another link in the navbar it's not changing color though hovering is working perfectly. Here is my html code :

<html>
<body>
<div class="collapse navbar-collapse justify-content-between" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 ">
<li class="nav-item ">
    <a class="nav-link scroll active " aria-current="page" href="#intro">Home</a>
</li>
<li class="nav-item">
    <a class="nav-link scroll" href="#about">About Us </a>
</li>
<li class="nav-item">
    <a class="nav-link scroll" href="#services">Services</a>
</li>
<li class="nav-item">
    <a class="nav-link scroll" href="#">Products</a>
</li>
</ul>
</div>
</html>
</body>

enter image description here

Here is my css:

<style>
.navbar .nav-item a {
  color: #5cbf8f !important;
}

.navbar .nav-item a:hover {
  color: #028b77 !important;
}

.navbar .nav-item a.active {
  color: #028b77 !important;
}
</style>

enter image description here

Here is my Js :

<script>
$(document).ready(function () {
  var scrollLink = $(".scroll");

  // Smooth scrolling
  scrollLink.click(function (e) {
    e.preventDefault();
    $("body,html").animate(
      {
        scrollTop: $(this.hash).offset().top,
      },
      1000
    );
  });

  // Active link switching
  $(window).scroll(function () {
    var scrollbarLocation = $(this).scrollTop();

    scrollLink.each(function () {
      var sectionOffset = $(this.hash).offset().top - 20;

      if (sectionOffset <= scrollbarLocation) {
        $(this).parent().addClass("active");
        $(this).parent().siblings().removeClass("active");
      }
    });
  });
});
</script>

enter image description here

N.B: Don't really know about js, just found it on the internet.

Navbar:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think your js is wrong.

$('.active').removeClass('active');
$(this).addClass('active');

More efficient options are available if the DOM hierarchy is known.

For example, if they're all siblings, you can use this:

$(this).addClass('active').siblings('.active').removeClass('active');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!