All,
I have two selects that look like this:
<select name='input_34' id='input_1_34' class='small gfield_select' tabindex='1' >
<option value='29' >Alabama</option>
<option value='34' >Alaska</option>
<option value='42' >Arizona</option>
....
<select name='input_13' id='input_13' class='small gfield_select' tabindex="2">
<option value='-1' selected='selected'>Select a base</option>
<option class="level-0" value="29">Alabama</option>
<option class="level-1" value="30"> Anniston Army Depot</option>
<option class="level-1" value="333"> Fort Rucker</option>
<option class="level-1" value="32"> Maxwell-Gunter AFB</option>
<option class="level-1" value="33"> Redstone Arsenal</option>
<option class="level-0" value="34">Alaska</option>
<option class="level-1" value="35"> Eielson AFB</option>
<option class="level-1" value="36"> Elmendorf AFB</option>
<option class="level-1" value="37"> Fort Greely</option>
<option class="level-1" value="38"> Fort Richardson</option>
<option class="level-1" value="39"> Fort Wainwright</option>
<option class="level-1" value="40"> USCG ISC Kodiak</option>
<option class="level-1" value="41"> USCG Juneau</option>
<option class="level-1" value="42"> USCG Ketchikan</option>
<option class="level-0" value="43">Arizona</option>
What I want to do is select a state in the first dropwown, and then remove all the options in the second dropdown, except those listed under the selected state.
What I've done so far is determine the selected state from the first dropdown, then get the index for where that option is in the second dropdown. How should I proceed from there? I was thinking about using .next() to go through each option with class .level-1, stopping at .level-0. Once I have that I can use .remove() to remove all other options except those.
Here is my code so far:
jQuery(document).ready(function(){
jQuery('#input_1_34').bind('change', function()
{
//get selected value from drop down
var selectedValue = jQuery("#input_1_34").val();
//hide everything but bases for the selected state
//jQuery("#input_13 option[value=" + selectedValue + "]").next().remove();
selectedIndex = jQuery("#input_13 option[value=" + selectedValue + "]").index();
alert (selectedIndex);
});
});
<optgroup>elements as I have in my updated answer.