Search code examples
javascriptjqueryhtmlmenuhref

How do I create an exception for a link my "active" scroll menu in jQuery?


I've got this code that goes through all my internal hrefs but I can't see to be able to figure out how to create an exception for one of the links that links to the div #section1. I need some kind of if statement surrounding the section.addClass("active"); line but I don't know which conditions to use to achieve this. Can you help?

Code: http://jsfiddle.net/vD3vP/

    if (scrolledTo > target - threshold && scrolledTo < target + threshold) {

        //remove all selected elements
        sections.removeClass("active");

        //add current selected element.
        section.addClass("active");
    }

Solution

  • Like this:

    $('a[href^="#"]:not(#exception)').click(function (event) {
        //do stuff
    });
    

    Fiddle: http://jsfiddle.net/KyleMuir/vD3vP/1/

    EDIT: You can determine whether or not it is the exception like this:

    //Smooth scroll when user click link that starts with #
    $('a[href^="#"]').click(function (event) {
    
        if (!$(this).is('#exception')) {
            alert('Hi');
        }
    
        //prevent the browser from jumping down to section.
        event.preventDefault();
    
    });
    

    Fiddle: http://jsfiddle.net/KyleMuir/vD3vP/2/