I have two related drop-down lists, in which the contents in the second drop-down list depends on the selection made in the first one. For example, in the following HTML code, you will choose application method first. If you choose Aerial as the application method, then you will answer further question such as aerial size dist. Otherwise, you need to answer ground spray type.
So once the webpage is loaded, the two second level drop-down lists (aerial size dist., and ground spray type.) are hidden. They will appear only when related choice is made in the first one (application method).
I am able to achieve this feature in jQuery (below jQuery code). But my approach is pretty stupid. My question is:
Is there a way to select the whole row, without using counting its sequence (nth-child())? Can I choose the whole row, based on selecting an element ID ? For example, can I first select $('#id_A') and then expand my selection to the whole row?
Is there a better way (a loop?) to achieve this hide or show feature rather than comparing all the possible choices (($(this).val() == X) )?
Thanks!
Here is the HTML code, and the form is generated by Django:
<div class=articles>
<form method=GET action=_output.html>
<table align=center>
<tr><th><label for=id_application_method>Application method:</label></th><td><select name=application_method id=id_application_method>
<option value=>Pick first</option>
<option value=A>Aerial</option>
<option value=B>Ground</option>
</select></td></tr>
<tr><th><label for=id_A>Aerial Size Dist:</label></th><td><select name=aerial_size_dist id=id_A>
<option value=A1 selected=selected>A1</option>
<option value=A2>A2</option>
</select></td></tr>
<tr><th><label for=id_B>Ground spray type:</label></th><td><select name=ground_spray_type id=id_B>
<option value=B1 selected=selected>B1</option>
<option value=B2>B2</option>
</select></td></tr>
</table>
</form>
</div>
Here is the jQuery code:
<script type=text/javascript src= https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js></script>
<script>$(function() {
$(tr:nth-child(2)).hide();
$(tr:nth-child(3)).hide();
$('#id_application_method').change(function() {
($(this).val() == A) ?
$(tr:nth-child(2)).show() : $(tr:nth-child(2)).hide();
($(this).val() == B) ?
$(tr:nth-child(3)).show() : $(tr:nth-child(3)).hide();
});});</script>