Link to home
Start Free TrialLog in
Avatar of purplesoup
purplesoupFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

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

Avatar of purplesoup

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

Thanks
You are welcome.