purplesoup
asked on
jQuery can you assign a control to a variable?
Suppose I have a dropdown list like this:
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?
<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>
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
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>
ASKER
Thanks
You are welcome.
Create 2 dropdown controls with values for each mode (dropdown1 and dropdown2)
Dropdown2 initially hidden and disabled.
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.
Open in new window