Changing hidden field based on select box selection
We have the following:
- Select field called "favePet"
- Hidden field called "petRefCode"
Because we have no control over the server, we have to do this on the front end. We need to send a certain "petRefCode" based on the favorite pet selection:
- favePet is Dog, then petRefCode is 'domestic01'
- favePet is Cat, then petRefCode is 'domestic01'
- favePet is Hamster, then petRefCode is 'domestic02'
- favePet is Parrot, then petRefCode is 'domestic02'
- favePet is Spider, then petRefCode is 'domestic03'
- favePet is Goldfish, then petRefCode is 'domestic03'
- <input type="hidden" name="petRefCode " id="petRefCode " value="">
- <label for="favePet">Choose your favorite pet:</label>
- <select id="favePet" name="favePet">
- <option value="">--Please choose an option--</option>
- <option value="dog">Dog</option>
- <option value="cat">Cat</option>
- <option value="hamster">Hamster</option>
- <option value="parrot">Parrot</option>
- <option value="spider">Spider</option>
- <option value="goldfish">Goldfish</option>
- </select>
How can I submit a hidden field based on the selection? I was experimenting with this:
- var refCode={
- dog : "domestic01",
- cat : "domestic01",
- hamster : "domestic02",
- parrot : "domestic02",
- spider : "domestic03",
- goldfish : "domestic03"
- }
- $("#favePet").change(function(e) {
- var [ petRefCode] = e.target.value;
- if ( e.target.value ) {
- $('#petRefCode').val(refCode[petRefCode]);
- } else {
- $('#petRefCode').val('');
- }
- })