By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

How do I un-select a drop-down selection when a checkbox is un-checked?

P: 11
I have a checkbox that when selected it fills in the shipping address(to match the billing address. When un-checked it should clear all the fields in the shipping address section of the form. It works up until the drop downs. I have tried multiple ways of formatting it but i can't seem to get it to clear once the checkbox is un-checked. Any assistance would be great.

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var shipName = "";
  3. var shipStreet = "";
  4. var shipStreet2 = "";
  5. var shipCity = "";
  6. var shipcountrySelect = 0;
  7. var shipProvState = 0;
  8. var shipPostalZip = "";
  9. var shipDayPhone1 = "";
  10. var shipDayPhone2 = "";
  11. var shipDayPhone3 = "";
  12. var shipCellPhone1 = "";
  13. var shipCellPhone2 = "";
  14. var shipCellPhone3 = "";
  15. var shipEmail = "";
  16.  
  17. function FillFields(box) {
  18.  if(document.bookform.box.checked) {
  19. document.bookform.shipName.value = document.bookform.billName.value;
  20. document.bookform.shipStreet.value = document.bookform.billStreet.value;
  21. document.bookform.shipStreet2.value = document.bookform.billStreet2.value;
  22. document.bookform.shipCity.value = document.bookform.billCity.value;
  23. document.bookform.shipCountrySelect.selectedIndex = document.bookform.billCountrySelect.selectedIndex;
  24. document.bookform.shipProvState.selectedIndex = document.bookform.billProvState.selectedIndex;
  25. document.bookform.shipPostalZip.value = document.bookform.billPostalZip.value;
  26. document.bookform.shipDayPhone1.value = document.bookform.billDayPhone1.value;
  27. document.bookform.shipDayPhone2.value = document.bookform.billDayPhone2.value;
  28. document.bookform.shipDayPhone3.value = document.bookform.billDayPhone3.value;
  29. document.bookform.shipCellPhone1.value = document.bookform.billCellPhone1.value;
  30. document.bookform.shipCellPhone2.value = document.bookform.billCellPhone2.value;
  31. document.bookform.shipCellPhone3.value = document.bookform.billCellPhone3.value;
  32. document.bookform.shipEmail.value = document.bookform.billEmail.value;
  33. }
  34. else {
  35. document.bookform.shipName.value = shipName;
  36. document.bookform.shipStreet.value = shipStreet;
  37. document.bookform.shipStreet2.value = shipStreet2;
  38. document.bookform.shipCity.value = shipCity;
  39. document.bookform.shipcountrySelect.value = shipcountrySelect.options[0].selected = true;
  40. document.bookform.shipProvState.value = shipProvState.options[0].selected = true;
  41. document.bookform.shipPostalZip.value = shipPostalZip;
  42. document.bookform.shipDayPhone1.value = shipDayPhone1;
  43. document.bookform.shipDayPhone2.value = shipDayPhone2;
  44. document.bookform.shipDayPhone3.value = shipDayPhone3;
  45. document.bookform.shipCellPhone1.value = shipCellPhone1;
  46. document.bookform.shipCellPhone2.value = shipCellPhone2;
  47. document.bookform.shipCellPhone3.value = shipCellPhone3;
  48. document.bookform.shipEmail.value = shipEmail;
  49.    }
  50. }
  51. </script>
  52.  
Thanks
Mar 9 '11 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
Assuming the first option is the default empty option, set the select object selectedIndex to 0:
Expand|Select|Wrap|Line Numbers
  1. selObj.selectedIndex = 0;
Mar 9 '11 #2

P: 11
I've tried that but I'm just not sure how exactly to do that within my code and have it work.
Mar 9 '11 #3

acoder
Expert Mod 15k+
P: 16,027
Change lines 39/40 to be like 23/4, but replace the billing part with 0, e.g.
Expand|Select|Wrap|Line Numbers
  1. document.bookform.shipCountrySelect.selectedIndex = 0;
Mar 10 '11 #4

P: 11
Thanks! That's was what i was looking for :)
Mar 10 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.