What is the most efficient way of showing/hiding form fields based on the value of a select control? I have one select control and three input fields, which show/hide based upon what type of business you work for.
<select id="business-type">
<option value="1">Sole Trader</option>
<option value="2">Partnership</option>
<option value="3">Public Sector</option>
<option value="4">Public Limited Company (Plc)</option>
<option value="5">Charity</option>
</select>
// This should appear if you are a Sole Trader (option 1 in the select box)
<div id="soletrader">
<label for="tax">VAT Number</label>
<input type="text" name="tax" id="tax" />
</div>
// This should appear if you are a Public Sector business (option 3 in the select box)
<div id="publicsector">
<label for="urn">URN Number</label>
<input type="text" name="urn" id="urn" />
</div>
// This should appear if you are a Charity (option 5 in the select box)
<div id="charity">
<label for="charityref">Charity Reference No.</label>
<input type="text" name="charity" id="charityref" />
</div>
Would a case statement – written in jquery – be the most appropriate approach to this matter? How would I write such a statement?
The three input fields need to be hidden by default. Any assistance would be greatly appreciated. I am dismal at javascript/jquery.
First, you should add “class=’field'” to each field div.
Then, with this code, you will show/hide the appropriate field upon selction in the dropdown: