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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:27:19+00:00 2026-06-11T15:27:19+00:00

Hi every one I’v been learning jquery for about 3 months now and I

  • 0

Hi every one I’v been learning jquery for about 3 months now and I cant figure this out.

What I am trying to do is change the background of a div (.box) by a click. I know how to do that for one click but I don’t know how to change the backgrounColors upon different clicks. I would like to implement some sort of an if statement that says that if the background of the current class==red than on click make the new current class yellow. In this example I can’t get the background color to yellow and I really want to be yellow upon the third click of the div. in the future I like the 4th click create even another backgroundColor.

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

     <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
     <head>
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
     <style type="text/css">
        .box{
            width:200px;
            height:200px;
            background-color:green;
        }
        .red{
                width:200px;
            height:200px;
            background-color:red;
        }
        .yellow{
            width:200px;
            height:200px;
            background-color:yellow;
        }
     </style>
    <script type="text/javascript">
        $(document).ready(function(){
        $('.box').click(toRed)
        function toRed(e){
            $(this).addClass('red');
        }
        if($('this').hasClass('red')){
            $(this).removeClass('red')
            .addClass('yellow');
        }

            });

    </script>
    </head>
    <body>
   <div class="box"></div>
    </body>
    </html>

so in conclusion im basically trying to change the backgroundcolor of the div when I click on it multiple times to different distinct colors each time.
and why doesn’t my code work.

Thank you for reading and thanks for your response in advance.

  • 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-11T15:27:20+00:00Added an answer on June 11, 2026 at 3:27 pm

    Are you looking for something like this jsFiddle example?

    $('.box').click(toRed)
    function toRed(e) {
        if ($(this).hasClass('red')) {
            $(this).removeClass('red').addClass('yellow');
        }
        $(this).addClass('red');
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

every one. I'm trying to figure out how to play live stream using MPMoviePlayerController.
Hello every One i am trying to get Ist date of All months in
Hi every One I am trying to add column in Mysql. My Sql Query
Hello every one I want to ask a question about file transfer using sockets.
Hello every one this is extension to my previous question.Here is the link File
hi to every one i am new in jquery. i want to make an
hi every one I'm using this code, through this I'm getting wrong time,i need
every one, i have been doing some work on rendering grass using billboard textures
every one i am trying to get the video frame or thumbnail from a
hello every one i face this problem and iam too weak in DB i

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.