Peter The Angular Dude
Peter The Angular Dude

Reputation: 1188

Changing menu options is NOT changing the active class even though I followed the solution in the link below

So, the website I'm working on is: http://wtr2022.webparity.net/. The link for the example is below.

How to change active class while click to another link in bootstrap use jquery?

So, I'll show the code I'm using and wrote and where to find it in the JS file.

HTML: The below starts on line 77 when you open the DEBUG console or scroll to the tag.

            <nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light sticky-top" id="ftco-navbar">
                <div class="container">

                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav" aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="fa fa-bars"></span> Menu
                    </button>
                    <div class="collapse navbar-collapse" id="ftco-nav">
                        <ul class="navbar-nav mr-auto">
                            <li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
                            <li class="nav-item"><a href="#aboutus" class="nav-link">About</a></li>
                            <li class="nav-item"><a href="#serviceslink" class="nav-link">Services</a></li>
                            <li class="nav-item"><a href="#projectslink" class="nav-link">Project</a></li>
                            <li class="nav-item"><a href="#bloglink" class="nav-link">Blog</a></li>
                            <li class="nav-item"><a href="#roofinglink" class="nav-link">Roofing</a></li>
                            <li class="nav-item"><a href="#contactlink" class="nav-link">Contact</a></li>
                        </ul>
                        <div class="d-flex align-items-center justify-content-center">
                            <img src="images/logos/weathertight-logo.png" class="logosmaller" alt=""/>
                            <!-- <span class="flaticon-roof-2"></span> -->
                        </div>
                        <div class="col-3 d-flex justify-content-end align-items-center">
                            <div class="social-media">
                                <p class="mb-0 d-flex">
                                    <a href="#" class="d-flex align-items-center justify-content-center"><span class="fa fa-facebook"><i class="sr-only">Facebook</i></span></a>
                                    <a href="#" class="d-flex align-items-center justify-content-center"><span class="fa fa-twitter"><i class="sr-only">Twitter</i></span></a>
                                    <a href="#" class="d-flex align-items-center justify-content-center"><span class="fa fa-instagram"><i class="sr-only">Instagram</i></span></a>
                                    <a href="#" class="d-flex align-items-center justify-content-center"><span class="fa fa-dribbble"><i class="sr-only">Dribbble</i></span></a>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </nav>
            <!-- END nav -->

JS: The below starts on line: 194 or scroll until you find this code.

        $('.navbar-nav li a').click((e) => {

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

            console.log("Argument for CLICK FUNCTION: ", e.currentTarget.innerHTML);
            console.log("CURRENT TARGET for CLICK FUNCTION: ", e.currentTarget.parentElement);

            if (e.currentTarget.innerHTML === "Services") {
                funcs.changeAboutImage('sprayfoam');
            }

            // add the active class to the link we clicked
            // console.log("THIS: ", $(this));

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

            // e.preventDefault();
        });

        /* Code for changing active link on clicking */
        let btns = $("#ftco-nav .navbar-nav .nav-link");

        for (let i = 0; i < btns.length; i++) {
            btns[i].addEventListener("click",
                    () => {

                let current = document.getElementsByClassName("active");
                console.log("CURRENT: ", current);
                // current[0].className = current[0].className.replace(" active", "");
                // this.className += " active";
            });
        }

        /* Code for changing active
         link on Scrolling */
        $(window).scroll(() => {
            let distance = $(window).scrollTop();

            // console.log($(window).scrollTop());

            if ($(window).scrollTop() > 701) {
                $('#ftco-navbar').addClass('navbar-fixed');
            }
            if ($(window).scrollTop() < 700) {
                $('#ftco-navbar').removeClass('navbar-fixed');
            }

            $('.ftco-section').each(function (i) {

                if ($(this).position().top <= distance + 250) {
                    $('.navbar-nav a.active').removeClass('active');
                    $('.navbar-nav a').eq(i).addClass('active');
                }
            });
        }).scroll();

Here's what happens:

FIRST: When I click on the FIRST SLIDE, "Learn more..." button, it needs to scroll to the GET A QUOTE section so the YELLOW bar with the words, GET A QUOTE appear. It's not.

SECOND: If you start from the base URL http://wtr2022.webparity.net/ and SCROLL until the menu appears, you'll see HOME as active. BUT, when you click on About, or any other menu, the active menu fails to become highlighted.

THIRD When you start at the base URL and SCROLL to "Roofing Services We Offer", below, you'll see SPRAY FOAM ROOFING already active, BUT, whenever you click on a menu, the ACTIVE spray foam menu and content is not active and nothing is there UNLESS you click on it.

enter image description here

This is what happens when you CLICK on a menu and scroll to Roofing Services We offer:

enter image description here

I think that the ACTIVE menu is TIED together, meaning, when the addEventListener() is hit, it REMOVES ALL active classes from everything, hence, the reason.

If anyone can assist me, I'd appreciate it. Thank you.

Upvotes: 0

Views: 605

Answers (1)

kmoser
kmoser

Reputation: 9308

Your click handler on the <a> tags is adding/removing the .active class to the <a> tag but it should be doing it to the parent <li> tag:

$('#ftco-nav .navbar-nav li a').click((e) => {
  $('#ftco-nav .active').removeClass('active');
  $(e).parent().addClass('active');
  // ...
}

Your scroll handler has a similar problem. It should be adding/removing the .active class from the <li class="nav-item"> tags, not the <a> tags:

if ($(this).position().top <= distance + 250) {
  $('#ftco-nav .navbar-nav .nav-item.active').removeClass('active');
  $('#ftco-nav .navbar-nav a').eq(i).parent().addClass('active');
}

Upvotes: 1

Related Questions