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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:44:40+00:00 2026-06-01T22:44:40+00:00

<div class=example> Test </div> $(‘.example’).click(function(){ $(this).css(‘color’,’red’); }); When the code above get’s clicked, it

  • 0
<div class="example">
 Test
</div>

$('.example').click(function(){
 $(this).css('color','red');
});

When the code above get’s clicked, it will apply the .css. Now what I need is for another bit of code (let’s say $(this).css('color','blue');) to be applied, replacing the previous code when .example gets clicked a second time.

I’ve searched for this and askers seem to only need .show/.hide events which can be substituted with .toggle, which is obviously not the case here.

  • 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-01T22:44:41+00:00Added an answer on June 1, 2026 at 10:44 pm

    Since you may have many instances of class example, simply maintaining a state using a single variable is not feasible, what you can do is to maintain the state of each instance of example within itself:

    Define two css classes

    .example { background:blue }
    .example.red { background:red }
    

    Then your click method:

    $('.example').click(function(){
     $(this).toggleClass('red');
    });
    

    If you prefer not to define new css classes, you can use data(), to make sure that the state is exclusive within each .example, this is useful if you have many instances of .example

    $('.example').click(function() {
        var color = $(this).data('color');
        if(color != 'blue') {
           $(this).css('color', 'blue');
           $(this).data('color', 'blue');
        } else {
           $(this).css('color', 'red');
           $(this).data('color', 'red');
        }
    });
    

    http://api.jquery.com/data/

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

Sidebar

Related Questions

HTML: <div id=div_id><div class=div_class><p>test</p> </div> <div class=two> <p>asd</p></div> </div> CSS: #div_id p { color:red;
<div class=example> <div class=test style=display:none;>Text</div> </div> <div class=example> <div class=test style=display:none;>Some other text</div> </div>
<div class=test> <div class=example></div> </div> <div class=test> </div> How can I apply jQuery to
<div class=example>Some example text</div> <div class=example>Some more example text</div> How can I remove all
I have something like this currently: <div class = className style = position: absolute;
Let's say I do something stupid like this: <div class=my_class>fun wee!!</div> <span class=my_class><b class=my_class>Title</b></span>
Say for example I have a body with the class test and I want
Is there a way to me do this? <img id=example src=anything.jpg title=something class=abc />
Check out my example here: http://timkjaerlange.com/foobar/test.html I'm currently trying to apply a class to
I want to replace the class with the div text like this : This:

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.