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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:30:00+00:00 2026-05-28T23:30:00+00:00

I have two list items that, when clicked, should change classes from ‘.off’ to

  • 0

I have two list items that, when clicked, should change classes from ‘.off’ to ‘.on’. Only one element should be ‘.on’ at a time so when one is already turned on and the other is clicked both elements should change classes from ‘.off’ to ‘.on’ and vice versa. If a list item with a class of ‘.on’ is clicked it should change classes to ‘.off’

The problem I am having is when a list item with class ‘.on’ is clicked it still runs the click function as if it had a class of ‘.off’

My html:

<ul>
    <li><a href="about" class="off">ABOUT</a></li>
    <li><a href="upload" class="off">SUBMIT</a></li>
</ul>

My javascript (running on jQuery 1.7.1)

$('.off').click(function(event) {
    event.preventDefault();
    $(".on").addClass("off").removeClass("on");
    $(this).addClass("on").removeClass("off");
});
$('.on').click(function(event) {
    event.preventDefault();
    $(this).addClass("off").removeClass("on");
});

Does anyone know what is going on here? Is there something wrong in my code or have I encountered some sort of bug here?

http://jsfiddle.net/ZC3CW/6/

  • 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-05-28T23:30:00+00:00Added an answer on May 28, 2026 at 11:30 pm

    The selectors you’re using to bind the event using click() are used to select the elements to add the event handler to. The selector is not considered when the handler is run.

    You should be looking for something more like this:

    $('li').click(function(event) {
        event.preventDefault();
    
        if ($(this).hasClass('off')) {
            $(".on").addClass("off").removeClass("on");
            $(this).addClass("on").removeClass("off");
        } else { // $(this).hasClass('on');
            $(this).addClass("off").removeClass("on");
        }
    });
    

    You might want to make the li selector more explicit by adding a class/id to the ul or li‘s.

    To confuse things further, you could also do this (if you’re using jQuery > 1.7);

    $(document).on('click', '.off', function(event) {
        event.preventDefault();
        $(".on").addClass("off").removeClass("on");
        $(this).addClass("on").removeClass("off");
    });
    $(document).on('click', '.on', function(event) {
        event.preventDefault();
        $(this).addClass("off").removeClass("on");
    });
    

    This is because the .on() function works by attaching the event handler to the selected elements (document), and will only execute the handler (the function) on the event specified (click) if the element that the event originated from matches the selector .off at the time the event fired, not at binding time.

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

Sidebar

Related Questions

I have two classes Company CompanyKey CompanyName Person FirstName LastName CompanyKey The list items
If I have two objects, one being the list of items, and the other
I have two GridViews that list out included and exclude data items respectively. By
I have two string lists that I'm working with. One that has a list
I have two list that I load when my app starts. The first one
I have two Activities that both list items in a table. For the time
I have two list on my request on jsp. First one is productGroupName, and
I have two classes: Media and Container. I have two lists List<Media> and List<Container>
i have a asp:radio button with two list items asp:RadioButtonList id=Myradio asp:listitem1 value=li1 asp:listitem2
So I have a custom ListView object. The list items have two textviews stacked

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.