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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:10:22+00:00 2026-05-25T22:10:22+00:00

I want to change background color each time when my_div recive class ‘selected’. So

  • 0

I want to change background color each time when my_div recive class ‘selected’.
So far I’ve got this:

$(document).ready(function() {
    if ($('#my_div').hasClass('selected')) {
        $("body").css({backgroundColor: '#111'});
    }
});

but it don’t work.

What’s wrong?

  • 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-25T22:10:23+00:00Added an answer on May 25, 2026 at 10:10 pm
    I want to change background color each time when my_div recive class 'selected'.
    

    There is a piece of code which is giving your element the selected class. This piece of code effectively changes your element’s class to be class = "whatever classes previously existed insertednewclass".

    One way to do what you’re trying to do, is to find the function which is adding/removing the class, and hook into it, for example:

    myFunction = function(...) {
        $('#my_div').addClass('selected');
        // add more code
        $('#my_div').css({backgroundColor:...});
    }
    

    I assume your case is not as simple as this. However this is possible even if the function is in an external library, though it’s risky if the library changes its internal behavior.

    // some.function is the function which adds the "selected" class
    var oldFunction = some.function;
    some.function = function() {
        return oldFunction.apply(this, arguments);
    }
    

    If you cannot do that and MUST reactively detect a class attribute modification, you can use the deprecated DOM Level 2 mutation events http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents which are vaguely supported in non-IE browsers. You can see if it is supported via $.bind in jQuery; if it isn’t supported in jQuery, you can use your_element.addEventListener(...) which is the native way to do things (which is basically all that jQuery is using under the covers).

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

Sidebar

Related Questions

I want to change the background color of our apps main window when a
I want to change the background color of the table cell when radio button
I don't want to change the entire background color of solution explorer in VS,
I want to change the background color of an tr element, that contains a
So I have a ListView and I want to change the color of each
I want to change the background color of li element to be changed when
I have an IValueConverter class that is used to change the background color of
I wan't to change the background color of a div dynamicly using the following
Specifically I want to change the text and background colors. Is it possible to
I have style sheet with a class name changebackgroundcolor i want make change in

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.