JQuery set active link

0 votes

i have a horizontal CSS Menu with this JQuery code:

function setActiveLink(target) {
        $(".MenuLink").removeClass("active");
        $('a[href=' + target + ']').addClass("active");
        //$('.tabcontent').addClass('hide');
        $(target).removeClass('hide');
    }

    $(document).ready(function(){
    $('.MenuLink').click(function(e) {
        e.preventDefault();
        var target = $(this).attr('href');
        document.location.hash = target;
        setActiveLink(target);
    });
    });

i want to set an active class on the clicked link (and remove all other active classes)

also, i want to be able to add the active class on the parent item when a sub menu item is clicked.

i tried creating the above function, but it doesnt add the class

Jul 20, 2022 in Web Development by gaurav
• 23,260 points
677 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Use some CSS:

.navbar-list .active a {  
 text-decoration: none;  // remove underline 
 // maybe more here, I don't know your intent
}

You may need a bit more CSS if e.g. you don't want colors for active/visited/hover, etc.

You can simplify your code a bit too. Your selectors are over-specified. I seldom use .parent() it's too fragile in case you change your HTML structure. I prefer to use .closest().

answered Jul 25, 2022 by rajatha
• 7,680 points

edited 4 days ago

Related Questions In Web Development

0 votes
0 answers

How to add a link in Jquery PrettyPhoto to download the image

I am using Jquery PrettyPhoto to have ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
992 views
0 votes
1 answer

How to set jQuery draggable min/max-left and min/max-right

How to set jQuery draggable min/max-left and ...READ MORE

answered Jul 25, 2022 in Web Development by rajatha
• 7,680 points
742 views
0 votes
1 answer

How to set jQuery draggable min/max-left and min/max-right

How to set jQuery draggable min/max-left and ...READ MORE

answered Jul 25, 2022 in Web Development by rajatha
• 7,680 points
1,272 views
0 votes
0 answers

JavaScript or jQuery "Are you sure?" dialog for <A> link?

I have a link in my HTML: <a ...READ MORE

Jul 26, 2022 in Web Development by gaurav
• 23,260 points
495 views
0 votes
1 answer

How to set selected value of jQuery Select2?

To set selected value of jQuery Select2 ...READ MORE

answered Aug 5, 2022 in Web Development by rajatha
• 7,680 points
7,204 views
0 votes
0 answers

set text in jquery

If I want to set the text ...READ MORE

Aug 8, 2022 in Web Development by gaurav
• 23,260 points
570 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,525 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,840 points
1,186 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
16,481 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,761 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP