Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Double function or?

Hey guys... I have this script that is looking for ".main-header .container #menu-center" to apply this "active" class when page is scrolled to certain part of content... But I want to target two elements without adding two separate JS files just for that purpose, I tried few things but it always make just one of those two work...

Can you help me out?

$(document).ready(function () {
    $(document).on("scroll", onScroll);

    //smoothscroll
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");

        $('a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');

        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});

function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('.main-header .container #menu-center a').each(function () {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('.main-header .container #menu-center ul li a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

I believe you want to use find() before you use each() ...

$('.main-header .container #menu-center').find('a').each(function () {

I'm not sure we understood each other...

This script is working fine, but I want it to work on ".otherelement #floatnavigation" as well?

Jeff Lemay
Jeff Lemay
14,268 Points

Oh, alright. Are you opposed to adding an additional class to your html? You could just do:

$('.toggle-active a').each(function () {
    // actions
}

You'd just add a class of 'toggle-active' to #menu-center and #floatnavigation. I'm not sure how to run the each loop on two different elements (that don't share a class) without duplicating your anonymous function.

Yeah, I would have to go with Jeff Lemay on this. Just add a unique class to the two elements you want to target.