1

I am having a problem using jQuery, merely due to inexperience using it. My program is meant to give the CSS class current to the links in my navbar if they are clicked, and remove the class from the previous owner of it.

Keep in mind I am very inexperienced in javascript, only picking it up in a few minutes for the sake of a school assignment.The script is simply not doing anything.

My Code:

$(document).ready(function() {
    $('a').click( function(i){
        var $current = $('a.current');
        $(this).addClass('current');
        $current.removeClass('current');
    });
});

Edit 1: Strange bug, current class is applied to the whole document if I do not click a link, but instead click the document.

5
  • what is the problem? Commented Aug 11, 2016 at 8:46
  • Are you wanting the class to be applied on the page after you navigate to it? Or do the links not actually point to anywhere? Commented Aug 11, 2016 at 8:59
  • The topic has already been answered, but, for the sake of argument, the links do point somewhere, they're part of my navbar. I want the class to be applied when the links are clicked. Commented Aug 11, 2016 at 9:01
  • Well if you want the page that is navigated to, to have the current class, then you need a way of passing that through to the new page, as all the answers so far will only work for the current page Commented Aug 11, 2016 at 9:03
  • The links are links that take you to certain elements with ids on the same page, they aren't inter-page, because that could just be done by setting that page's nav bar link to the current class Commented Aug 11, 2016 at 9:19

3 Answers 3

2

You should first remove the class, and then add it. Otherwise you will not have a class added if you click an anchor twice.

$(function() {
    $('a').click(function() {
        $('a.current').removeClass('current');
        $(this).addClass('current');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

You need to first remove the active class, then add the current class for the current element.

Comments

0

You need to narrow down your code to only affect the links in the nav bar, as currently, you are targetting all <a> tags.

var navLink = $('.nav a');

navLink.on('click', function(e){
    navLink.removeClass('current');
    $(this).addClass('current');
});

In this code, the variable gets all instances of the nav links, and if one of them is clicked, will remove the current class from all of the links before adding it onto the one that was clicked.

This jsfiddle will show you it in action: https://jsfiddle.net/td48vcqy/

2 Comments

Thank you very much, this was extremely helpful, only one problem with your code. I think it was meant to be $('a.nav'); not $('.nav a');
I just used that as an example of how to make your code specific to your nav. Ideally you would have a class either around your links that you can use or on the links themselves which would allow you to select the correct items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.