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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:15:45+00:00 2026-06-10T15:15:45+00:00

How can I apply a CSS background color to only the selected option? If

  • 0

How can I apply a CSS background color to only the selected option? If I apply a class to the select object, it styles all options. If I apply the desired classes to the options, I can see the styling fine for all the options when the select box is expanded. But when the select box is collapsed, the styling for the selected option is gone. I observe this behavior in the latest versions of Chrome and Firefox.

Here is some super basic example code. Without the jQuery, the selected option always appears unstyled. With the jQuery, once a “modified” option is selected, all options are styled as “modified”. I haven’t been able to figure out a way around this… Any ideas?

I do not want to change the styling of the options. Only to be able to see the styling of any given option when it is selected without overriding the styling of the others.

<style>
  select.modified,
  option.modified{
    background-color: red;
  }
</style>

<select id="example">
  <option value="bird" class="modified">bird</option>
  <option value="cat">cat</option>
  <option value="dog" class="modified">dog</option>
</select>

<script>
  $('#example').on('change',function(){
    $(this).prop('class',$(this).find('option:selected').prop('class'));  
  });
</script>
  • 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-10T15:15:47+00:00Added an answer on June 10, 2026 at 3:15 pm

    I have found the best workaround for this issue. Basically I have to remove any classes from the selected option on focus, and add it back those classes on blur. This seems to do the trick fairly well.

    <script>
      $('#example').on('change',function(){
        // do whatever needs to be done
        $(this).blur();
      }).on('focus',function(){
        $(this).removeClass($(this).find('option:selected').prop('class'));
      }).on('blur',function(){
        $(this).addClass($(this).find('option:selected').prop('class'));
      });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scriptaculous's Effect.Morph can take a CSS class and apply it over a period of
I want to apply some CSS to text that I can't get marked up
I can't get CSS to apply to the function below. Is it because it's
I understand I can apply several options to the ServiceContract (like Name , Namespace
How do I apply a class to the following so that I can apply
I have this code : if (pansel.val() == 1) $(#myDiv).css('background-image', 'url(/private_images/guida_check_category.jpg)'); else $(#myDiv).css({ 'background-color':
Which css should I apply to textbox that make it same as background means
Currently I am able to apply custom background color for odd number rows. My
I use jQuery's .css() method to apply styles to an element. I do this
If i have a div with this kind of CSS: background: #000000; Can 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.