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 8016241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:29:09+00:00 2026-06-04T20:29:09+00:00

I have a list with 3 items, and I have made a jQuery click

  • 0

I have a list with 3 items, and I have made a jQuery click event to add a css class to it.
But when I click one of the items the class attribute is set but disapperas directly after.

Any ideas why?

Code:

<ul>
    <li><a id="home" href="home.php">Home</a></li>
    <li><a id="apps" href="apps.php">Apps</a></li>
    <li><a id="support" href="support.php">Support</a></li>
</ul>
$(document).ready(function() {
    $('#home').click(function (){
        $(this).addClass('btnActive');
    });
});
  • 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-04T20:29:11+00:00Added an answer on June 4, 2026 at 8:29 pm

    Updated Answer:

    When you follow a link, the entire DOM of the page is torn down and thrown away, and replaced by the DOM created by reading the HTML of the new page. So changes you make to the old DOM don’t persist. If you want the “btnActive” class to be on the link when the page is loaded, you’ll need to modify the HTML of the home.php, apps.php, and support.php pages accordingly.

    E.g., for home.php:

    <ul>
        <li><a id="home" href="home.php" class="btnActive">Home</a></li>
        <li><a id="apps" href="apps.php">Apps</a></li>
        <li><a id="support" href="support.php">Support</a></li>
    </ul>
    

    …but for apps.php:

    <ul>
        <li><a id="home" href="home.php">Home</a></li>
        <li><a id="apps" href="apps.php" class="btnActive">Apps</a></li>
        <li><a id="support" href="support.php">Support</a></li>
    </ul>
    

    Alternately, you could use JavaScript on page load to add the class based on the location.href, but really it’s better to modify the HTML. But just for completeness, since you use the same value for the id of the link and the name of the page, you could do this:

    $(document).ready(function() {
        var id = location.href.replace(/.*\/([\w]+)\.php$/, '$1');
        if (id) {
            $("#" + id).addClass("btnActive");
        }
    });
    

    That isolates the name of the page from the URL (ignoring any path leading up to it, and stripping off the .php at the end), then uses it as an id and adds the class. Again, I don’t recommend it, but with the URLs you quoted, it should work.

    The following original answer was from before I realized (thanks to Rory) that you probably really do need to follow the link.

    Original Answer:

    Because you never tell the browser not to follow the link, so the page gets reloaded.

    You can fix it by either doing return false; out of your event handler:

    $(document).ready(function() {
        $('#home').click(function (){
            $(this).addClass('btnActive');
            return false;  // <=== The new bit
        });
    });
    

    …or accepting the event argument and calling preventDefault on it:

    $(document).ready(function() {
        // Accept the arg ---------v
        $('#home').click(function (event){
            // call the preventDefault
            event.preventDefault();
            $(this).addClass('btnActive');
        });
    });
    

    return false in a jQuery event handler does two things:

    1. Prevents the default action (in this case, following the link).

    2. Stops the event propagating to ancestor elements.

    preventDefault just does the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of content items, each with a set width but different
In jQuery, I have a somewhat simple script with pre-made css classes, but no
I have a list which is dynamically built, but there are empty list items
I have a list of items of a custom class. That class contains an
I have a list of items. Each of them has a set of data
I have a list of items within a scrollable (jQuery tools)... It's shows 3
I have a list made as sortable using jQuery UI. The first list item
I have a list of strings made of 1.9-2 MILLION items . The following
Can someone suggest what I am doing wrong? Basically I have a List Items,
I have List with n items. I wish transform my list to new list,

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.