jQuery can you assign a control to a variable?

Suppose I have a dropdown list like this:

<select id="dropdown">
<option value="v1">option 1</option>
<option value="v2">option 2</option>
<option value="v3">option 3</option>
<option value="v4">option 4</option>
<option value="v5">option 5</option>
</select>

Open in new window


and I have a radio button with "mode 1" and "mode 2".

When the radio button is in "mode 1" I want options 1, 2, 3 and 4 to appear in the dropdown list. When the radio button is in "mode 2" I want options 1, 3 and 5 to appear.

My idea is when the page first loads, I clone the dropdown into two variables dropdownMode1 and dropdownMode2, filter the values I want, then when the radio button is clicked, to assign the dropdown control to each variable.

Is there an easy way to do this rather than clearing the dropdown and going through each option and doing an append?
purplesoupAsked:
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.

F IgorDeveloperCommented:
If you don't want to append options dinamically, then you can try this:

Create 2 dropdown controls with values for each mode (dropdown1 and dropdown2)
Dropdown2 initially hidden and disabled.

<select id="dropdown1">
<option value="v1">option 1</option>
<option value="v2">option 2</option>
<option value="v3">option 3</option>
<option value="v4">option 4</option>
</select>
<select id="dropdown2" style="display:none" disabled>
<option value="v1">option 1</option>
<option value="v3">option 3</option>
<option value="v5">option 5</option>
</select>

Open in new window


when you are selecting mode2 show dropdown2 and enable it (disabling and hiding dropdown1). If you are sending a form, also you must set the "name" attribute in the enabled control, and remove the name attribute in the disabled control.

$("input[@name='radiobuttonName']").change(function(){

if ($(this).val()==2){
   $("#dropdown1").hide();
   $("#dropdown1").prop("disabled",true);
   $("#dropdown2").show();
   $("#dropdown2").prop("disabled",false);
}
if ($(this).val()==1){
 .... 
}



});

Open in new window

0
purplesoupAuthor Commented:
ok that is an option - but you're saying if you want to use JavaScript it is removing and appending? It was rather hoping there was a way to get a control to just assume all the properties of a variable...
0
Julian HansenCommented:
It is possible to do this by simply hiding / displaying options based on a class.

First set up select like this
  <select id="dropdown" class="form-control">
    <option value="v1" class="opt1 opt2">option 1</option>
    <option value="v2" class="opt1 opt2">option 2</option>
    <option value="v3" class="opt1 opt2">option 3</option>
    <option value="v4" class="opt1">option 4</option>
    <option value="v5" class="opt2">option 5</option>
  </select>

Open in new window

Next Add this JQuery
<script>
$(':radio').click(function() {
  var id = $(this).attr('id');
  $('#dropdown option').hide();
  $('#dropdown option.' + id).show();
});
</script>

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
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.

F IgorDeveloperCommented:
Note that in the Julian's example you can hide options, but this behavior is not working in some browsers (Internet Explorer does not hide options using css display). There are some variants to allow this behavior in IE.
0
Julian HansenCommented:
I personally discount IE as qualifying as a browser, however acknowledge that others are forced to use it.

The solution for IE is trivial if you are prepared to accept disabled options rather than hidden ones. By adding .prop('disabled', true) to the code below (to "hide" in IE) - the effect is the same on real browsers and in IE it disables the option so it is not selectable. Sample updated here (Tested back to IE8)
<script>
$(':radio').click(function() {
  var id = $(this).attr('id');
  $('#dropdown option').hide().prop('disabled',true);
  $('#dropdown option.' + id).show().prop('disabled', false);
});
</script>

Open in new window

0
purplesoupAuthor Commented:
Thanks
0
Julian HansenCommented:
You are welcome.
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
JavaScript

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.