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>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<style>
option.sel {
background: red;
}
</style>
<script type="text/javascript">

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

</script>
</head>
<body>

 <select id="s">
  <option>SELECT</option>
  <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>
 </select>

</body>
</html>

Open in new window

LVL 10
skijAsked:
Who is Participating?
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?
0
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
0
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

skijAuthor Commented:
When "1" is selected, ALL options with a value of "1" should be selected.
0
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?
0
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:
value="1"
0
Julian HansenCommented:
OIC in that case

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

    // REMOVE CLASS FROM PREV
    $('.sel').removeClass('sel');

    // GET THE SELECTED VALUE
    var val = $(this).val();

    // SET CLASS TO ALL OPTIONS THAT HAVE THIS VALUE
    $('#s option[value=' + val + ']').addClass('sel');
  });
});

Open in new window

Working sample here
0

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

0
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
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.