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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:21:40+00:00 2026-05-31T05:21:40+00:00

I am trying to work out, on click function remove class on and add

  • 0

I am trying to work out, on click function remove class on and add new class off. when I click on the div again I would like to add class on again and remove class off which is not happening. I am not sure what reason this is not working. But if alert it works.
Here is my code. and Jsfiddle demo

$(".on").bind('click',function() {
    $(this).removeClass('on').addClass('off');
    alert('Hello World');
})
$(".off").bind('click',function() {
    $(this).removeclass('off').addClass('on');
  alert('Hello World');
})
​
  • 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-31T05:21:41+00:00Added an answer on May 31, 2026 at 5:21 am

    $(".on") and $(".off") select elements currently in the DOM that match the selectors. So if you change the class of the element from .on to .off then it will still fire the .on event handler since that is what was bound to the element. You could use event delegation but I think .toggleClass() is probably a better implementation for you:

    $(".on, .off").bind('click',function() {
        $(this).toggleClass('on off');
        alert('Hello World');
    })
    

    Here is a demo: http://jsfiddle.net/LEvM4/1/

    If you need to know which class is currently on the element you can check with .hasClass():

    $(".on, .off").bind('click',function() {
        $(this).toggleClass('on off');
        if ($(this).hasClass('on')) {
            alert('Hello World');
        } else {
            alert('Goodbye World');
        }
    })
    

    Here is a demo: http://jsfiddle.net/LEvM4/2/

    A word about event delegation. You can also bind to an element that is an ancestor of the element you want to bind to, and delegate the event handler to the descendant element:

    $(document).delegate(".on", 'click', function() {
        $(this).addClass('off').removeClass('on');
        alert('Goodbye World');
    }).delegate(".off", 'click', function() {
        $(this).addClass('on').removeClass('off');
        alert('Hello World');
    })
    

    Here is a demo: http://jsfiddle.net/LEvM4/3/

    A downside to event delegation is that when an event fires it requires more overhead. It’s usually best to try and bind directly to an element if possible. Normally you use event delegation if an element is not currently present in the DOM but you want to bind to future instances of an element.

    Some documentation for ya:

    • .on(): http://api.jquery.com/on
    • .toggleClass(): http://api.jquery.com/toggleClass
    • .hasClass(): http://api.jquery.com/hasClass
    • .delegate(): http://api.jquery.com/delegate
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out an hide/show function according to, if the div's are
I am trying to work out the format of a password file which is
I'm trying to work out a trivial issue: var setListeners = function() { for(var
I am trying work out with MERGE statment to Insert / Update Dimension Table
I am trying to work out the best database model for the current setup:
I am trying to work out how to get the value of table cell
I'm trying to work out a way of passing the web current http context
I am trying to work out a code sample to demonstrate the debugging functionality
I'm trying to work out what a BizTalk call to a web service is
I'm trying to work out what the best method to passing a large number

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.