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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:03:23+00:00 2026-06-04T14:03:23+00:00

Is it possible to do an if statement in javascript/jquery to determine: If the

  • 0

Is it possible to do an if statement in javascript/jquery to determine:

If the value of a certain element’s css attribute is equal to a given value?

such as:

if( $('.box').css(background-color = blue) ){
    // do this...
}
  • 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-04T14:03:23+00:00Added an answer on June 4, 2026 at 2:03 pm

    The css JQuery method, when given only one parameter, will return the value for the given paramenter. You can try this:

    var color = $('.box').css('background-color');
    if (color == 'rgb(0, 0, 255)' || color == 'blue') // =='blue' <- IE hack
        alert("it's blue!\nColor detected: " + color);
    

    The above sample will only work for the first element in the JQuery selector, therefore you should use the .each JQuery method to iterate through the whole selector if you have more than one element in it.

    Please note that the .css JQuery method returns a RGB combination in most browsers (except IE), so testing it against a string such as blue will not suffice for non-IE browsers. You can test that in the JQuery API site and my fiddle.

    And here’s with the proper .each iteration:

    $('.box').each(function(i){
        var color = $(this).css('background-color');
        if (color == 'rgb(0, 0, 255)' || color == 'blue') // =='blue' <- IE hack
            alert("div " + i + " is blue!\nColor detected: " + color);
    });​
    

    JSFiddle


    Edit: over a year and half later, I feel like this answer deserves an update.

    For most use cases, I’d recommend using a CSS class:

    .blue {
        color: blue;
    }
    

    This allows the usage of the addClass(), removeClass(), toggleClass() methods for manipulation, as well as hasClass() for checking:

    if ( $('#myElem').hasClass('blue') ) {
        //it has the .blue class!
    }
    

    This works seamlessly in all browsers without needing hacks, and as a plus you get better separation of concerns — that is, if your styling ever changes, say .blue { color: lightblue; }, the JS will keep working without modifications.

    Note: of course, this approach is not suitable for a couple rare use cases where the color value is dynamically generated (e.g. using Math.random() or picking a color value off a canvas pixel), in these rare use cases you can still use the first solution in this answer.

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

Sidebar

Related Questions

Possible Duplicate: JavaScript asynchronous return value / assignment with jQuery I need a prototype
Possible Duplicates: Test for value in Javascript Array Best way to find an item
Possible Duplicate: jQuery equivalent of JavaScript's addEventListener method Also from a very good jQuery
is this possible? <div id=anything>I will change</div> <div id=id jq=$('#anything').css({background: 'red'})></div> var x =
Possible Duplicate: Is it safe to assume strict comparison in a Javascript switch statement?
Possible Duplicate: If statement in my loops are being skipped in C Having a
Possible Duplicate: Why is my comparing if statement not working? I am attempting to
Is it possible for a receive statement to have multiple timeout clauses, and if
Is it possible to have a SELECT statement in mySQL with an IF/ELSE clause
Is it possible to convert this if statement into a one line statement ?

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.