Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:49:05+00:00 2026-06-04T10:49:05+00:00

Jquery Removing & Adding ‘Active’ Class On LI Navigation Works With preventDefault, But Link

  • 0

Jquery Removing & Adding ‘Active’ Class On LI Navigation Works With preventDefault, But Link No Longer

Jquery removing an adding active class to li nodes not working

I know it should be really basic to remove and add the active class to li’s in order to highlight the current page’s nav item, but I’ve been unable to get it to work.

I find that I must use e.preventDefault() to make it work, but when I do the link doesn’t work but the highlighted nav item works. If I remove the preventDefault() then it navigates but doesn’t highlight. I should say that my nav is sitting in a header.php include file. Here’s what I have:

<ul class="myNav">
  <li><a href="index.php">Home</a></li>
  <li><a href="portfolio.php">Portfolio</a></li>
  <li><a href="about.php">About</a></li>
  <li><a href="contact.php">Contact</a></li>
</ul>

$('.myNav li a').click(function (e)
{
  e.preventDefault();
$('.myNav li').siblings().removeClass('active');
$(this).parent().addClass('active');
//var url = $(this).attr("href"); 
//window.location = url;
});

I tried to send the page to the clicked url and this loads the correct page, but it removes the class! I’d like to solve this, but I’d also like to understand why it isn’t working correctly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:49:06+00:00Added an answer on June 4, 2026 at 10:49 am

    I ended up doing it in PHP like this:

    <?php
    //grab the current page name to use in the
    $path = $_SERVER['PHP_SELF'];
    $page = basename($path);
    $page = basename($path, '.php');
    ?>
    
            <li<?php if ($page == 'index') echo 'class="active"';?>><a href="index.php">Home</a></li>
        <li <?php if ($page == 'portfolio') echo "class='active'"; ?> ><a href="portfolio.php">Portfolio</a></li>
        <li <?php if ($page == 'about') echo 'class="active"'; ?> ><a href="about.php">About</a></li>
        <li <?php if ($page == 'contact') echo 'class="active"'; ?> ><a href="contact.php">Contact</a></li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jQuery to show and hide a div by adding/removing a class
I'm adding and removing <li> elements with jQuery, that are shown horizontally with the
How would I go about hiding all HTML tags using jquery. Not removing but
I'm creating and removing HTML from inside a div with jQuery (shopping cart, adding/removing
jQuery 1.7.1 - I have an array of input text elements (class - pr)
Jquery question here. I would like to get the CLASS tag of the only
I am using jquery placeholder.js . Is there a possibility of hiding or removing
I have jQuery tab, it works perfectly, I want to make the tabs to
Possible Duplicate: Removing multiple classes (jQuery) how can we remove multiple classes from one
How do i go about removing # and @ from the variable using jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.