There is a code

$(".main-nav a").click(function() { var scrollId = $(this).attr("href"); change_active($(this), scrollId); scroll_if_anchor(scrollId); return false; }); function change_active(href, scrollId) { console.log(href); href .removeClass("active") .filter("[href=" + scrollId +"]").addClass("active"); } 

The class is set but not removed from the previous item - a, why?

    1 answer 1

    Because in this there is one particular element and not the previous selector.

    That's what you probably wanted:

     var href=$(".main-nav a"); href.click(function () { var scrollId = $(this).attr("href"); change_active($(this)); //scroll_if_anchor(scrollId); return false; }); function change_active(current) { console.log(href); href.removeClass("active"); current.addClass("active"); } 

    http://jsfiddle.net/oceog/P3c89/