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

  • Home
  • SEARCH
  • 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 8068815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:48:56+00:00 2026-06-05T12:48:56+00:00

I have written two jQuery functions, the simplified version of which run thus: $(‘button.class’).not(‘.selected’).on(‘click’,

  • 0

I have written two jQuery functions, the simplified version of which run thus:

$('button.class').not('.selected').on('click', function() {
    console.log('not selected');
    console.log($(this));
    $(this).addClass('selected');
}

$('button.class.selected').on('click', function() {
     console.log('selected');
     console.log($(this));
     $(this).removeClass('selected');
}

Clicking on the button always logs the following in the console:

not selected
[<button class="someClass1 someClass2 selected">Text</button>]

The Web Inspector shows this before clicking on the button:

<button class="someClass1 someClass2">Text</button>

and this after clicking on it:

<button class="someClass1 someClass2 selected">Text</button>

Clicking on it again changes nothing.

Switching addClass() to toggleClass() succeeds in toggling 'selected' on and off, but still always logs not selected, which suggests something is going on with my selectors. Why is the first function always being called even when 'selected' is clearly one of the classes associated with the button element?

I noticed in testing some solutions that hard-coding 'selected' as a class of button results in the opposite problem: only second of the functions is ever called, even though toggleClass() works as intended. Do classes added via jQuery not “count” as classes?

  • 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-05T12:48:57+00:00Added an answer on June 5, 2026 at 12:48 pm

    The CSS selector is evaluated at the time you run it. I wouldn’t expect that there are any elements matching $('button.class.selected') on page load, so your second handler is never executed, because it is never bound to anything.
    What you should be doing is attaching event to some base selector such as $('button.class') and then doing filtering when event is fired.

    $("button.class").on("click", function()
    {
        if ($(this).is(".selected"))
        {
            console.log('selected');
            console.log($(this));
            $(this).removeClass('selected');
        }
        else
        {
            console.log('not selected');
            console.log($(this));
            $(this).addClass('selected');
        }
    });
    

    Either version works, but one might be more appropriate than the other, depending on what are you actually trying to do.

    If all you are looking to do is to add/remove .selected on button click then this much simpler version will do the job

    $("button.class").on("click", function() { $(this).toggleClass("selected"); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a program written in C++ in which one or two functions
I have written two short tests and compiled both with g++ -S (gcc version
I have written two scripts where one script calls subprocess.Popen to run a terminal
I have written two pieces of code which I intended to have identical outputs,
I have written an app in C which expects two lines at input. First
I have written a module similar to the following: module One class Two def
I have a jquery function(This is not written by me anyway still I am
I have two buttons: btnAdd and btnUpdate . I have written a jquery function
This is written in an external .js with jquery... I have two windows that
I have written two functions setSes() and getSes() and here I have written session_start()

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.