jQuery: add a class to ALL options with selected value

Using jQuery, how can I add a class to ALL options that have the selected value?

<!DOCTYPE html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
option.sel {
background: red;
<script type="text/javascript">

$(document).ready(function () {
  $('#s').change(function () {
    $('#s option').removeClass('sel');
    $('#s option:selected').addClass('sel');


 <select id="s">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>


Open in new window

LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prakash SamariyaIT ProfessionalCommented:
Your current page is working fine with select only ONE value at a time as by default nature of html select tag.

Can you please elaborate more?
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
You need to add "select"

$(document).ready(function () {
  $('#s').change(function () {
    $('select#s option').removeClass('sel');
    $('select#s option:selected').addClass('sel');

Open in new window

Note: This will not work in chrome
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Working sample http://jsbin.com/xulaliwipu/1/edit?html,output

Once you select an option, then select again, your previous selection is in red.  Will not work in chrome/mac.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

skijAuthor Commented:
When "1" is selected, ALL options with a value of "1" should be selected.
Julian HansenCommented:
@Scott - not necessary to add the select the id identifies uniquely identifies the value.

The code you have posted works - not sure what the issue is.

One thing to note however the nature of a select box is that the selected item is highlighted when you drop the box down - this will mask any styling that you have applied to the option - once you move the mouse over another of the option the highlighting will become visible.

Are you asking how to change the highlight colour?
skijAuthor Commented:
With the code I posted, when "1" is selected, the background of "1" turns red.

Instead, when "1" is selected, I want the background of both "1" and" "One" to turn red, because both of them have:
Julian HansenCommented:
OIC in that case

$(document).ready(function () {
  $('#s').change(function () {


    var val = $(this).val();

    $('#s option[value=' + val + ']').addClass('sel');

Open in new window

Working sample here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Prakash SamariyaIT ProfessionalCommented:
Just comment your line as below
 //$('#s option:selected').addClass('sel');

Open in new window

and add line as below
$('#s option[value=' + $('#s').val() + ']').addClass('sel');

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.