You are right, IceDragon, it makes no sense to use a few if / else, because every time you add parameters, you will have to rewrite your code. There are several ways to avoid this. Here is just one approach:
<html><body> <form> <p> <input type="radio" name="seat" onclick="chose(this, 'Standard')" /> Standard seat <input type="radio" name="seat" onclick="chose(this, 'Wheelchair')" /> Wheelchair seat </p><p> <input type="radio" name="area" onclick="chose(this, 'BN')" /> Back Nave <input type="radio" name="area" onclick="chose(this, 'FN')" /> Front nave <input type="radio" name="area" onclick="chose(this, 'MN')" /> Middle nave </p> </form> <script> </script> </body></html>
Please note that in the selections object we track the choices the user has made so far. Then, when the user clicks on the image (or something else that launches the code you are working on), the function simply formats the values that have already been collected.
The only drawback of how I wrote this code is that browsers usually cache the state of the switches, so the radio button may already be selected, but the select () function has not been called. One quick and dirty workaround is to add an identifier to the form tag and run it when the page loads:
document.getElementById('form').reset();
Where 'form' is the attribute of the form tag ID.
source share