I have two drop down list; one with states and the other with cities. I want the second list with cities to show only the cities in the selected state. Why is this not working?
$(document).ready(function($) {
$("#state").change(function() {
var selected_state = $("#state").val(); //get the selected state
$("#city").children().hide(); //hide all the options
$("#selected_state").children().show(); //only show cities in selected state
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
State: <select name="state" id="state">
<option value="state1">State1</option>
<option value="state2">State2</option>
</select> City:
<select name="city" id="city">
<div id="state1">
<option value="0">City1a</option>
<option value="1">City1b</option>
<option value="2">City1c</option>
</div>
<div id="state2">
<option value="3">City2a</option>
<option value="4">City2b</option>
<option value="5">City2c</option>
</div>
</select>
<input name="submit" type="submit" value="Submit" />
</form>