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

restoring selections in radio buttons

100+
P: 110
I have a webpage that uses visible and hidden tables to create the effect of going from screen to screen. Because a user might want to go back and change previous selections I have a "previous page" button that goes back to a table that has been made hidden. Text fields retain their values; but, option buttons do not. I am having problems restoring the original selections to the option buttons. I can post the code; but, the javascript functions are very long. If I have a pair of option buttons where the options are Yes and No I assign values to the variable names based on whether or not the "checked" option is true (eg: document.screenOne.nearWater[0].checked=true;). However when I go back to the "page" I cannot restore the original selection by reversing the process. For example, using the example above, if option "0" is "No" the following code does not produce the correct result.
Expand|Select|Wrap|Line Numbers
  1. if(document.getElementById('nearWater').value=="No"){
  2. document.screenOne.nearWater[0].checked=true;
  3. } else if(document.getElementById('nearWater').value=="Yes"){
  4. document.screenOne.nearWater[1].checked=true;
  5. }
  6.  
How can I restore the original selections to radio buttons?
Feb 10 '09 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
If the "page" (div/table) is simply hidden (probably by setting style.display), then it shouldn't affect any elements within the container element. Can you show the code for when you hide a container element, e.g. the function called when clicking Next.
Feb 10 '09 #2

100+
P: 110
This is the code for the first "page"

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type="text/javascript">
  3. <!-- 
  4. function question(x){
  5.     var itemNumber = x; // question number
  6.  
  7. ...
  8.  
  9.     if(x=="1"){
  10.         var y = document.getElementById('pState'); 
  11.         var z = y.options[y.selectedIndex].value; //value from dropdown
  12.         document.getElementById('propertyST').value = z;
  13.  
  14.         if (z=='IN' || z=='IL' || z=='KY' || z=='MO' || z=='OH' || z=='TN'){
  15.             status="ELIGIBLE";
  16.         } else {
  17.             status="REFERRED";
  18.         }
  19.  
  20.             if(document.getElementById('pState').value=="AL"){
  21.                 document.getElementById('propertyState').value="Alabama";
  22.             } else if(document.getElementById('pState').value=="AZ"){
  23.                 document.getElementById('propertyState').value="Arizona";
  24.             } else if(document.getElementById('pState').value=="AR"){
  25.                 document.getElementById('propertyState').value="Arkansas";
  26.             } else if(document.getElementById('pState').value=="CA"){
  27.                 document.getElementById('propertyState').value="California";
  28.             } else if(document.getElementById('pState').value=="CO"){
  29.                 document.getElementById('propertyState').value="Colorado";
  30.             } else if(document.getElementById('pState').value=="CT"){
  31.                 document.getElementById('propertyState').value="Connecticut";
  32.             } else if(document.getElementById('pState').value=="DE"){
  33.                 document.getElementById('propertyState').value="Delaware";
  34.             } else if(document.getElementById('pState').value=="DC"){
  35.                 document.getElementById('propertyState').value="District of Columbia";
  36.             } else if(document.getElementById('pState').value=="FL"){
  37.                 document.getElementById('propertyState').value="Florida";
  38.             } else if(document.getElementById('pState').value=="GA"){
  39.                 document.getElementById('propertyState').value="Georgia";
  40.             } else if(document.getElementById('pState').value=="ID"){
  41.                 document.getElementById('propertyState').value="Idaho";
  42.             } else if(document.getElementById('pState').value=="IL"){
  43.                 document.getElementById('propertyState').value="Illinois";
  44.             } else if(document.getElementById('pState').value=="IN"){
  45.                 document.getElementById('propertyState').value="Indiana";
  46.             } else if(document.getElementById('pState').value=="IA"){
  47.                 document.getElementById('propertyState').value="Iowa";
  48.             } else if(document.getElementById('pState').value=="KS"){
  49.                 document.getElementById('propertyState').value="Kansas";
  50.             } else if(document.getElementById('pState').value=="KY"){
  51.                 document.getElementById('propertyState').value="Kentucky";
  52.             } else if(document.getElementById('pState').value=="LA"){
  53.                 document.getElementById('propertyState').value="Louisiana";
  54.             } else if(document.getElementById('pState').value=="ME"){
  55.                 document.getElementById('propertyState').value="Maine";
  56.             } else if(document.getElementById('pState').value=="MD"){
  57.                 document.getElementById('propertyState').value="Maryland";
  58.             } else if(document.getElementById('pState').value=="MA"){
  59.                 document.getElementById('propertyState').value="Massachusetts";
  60.             } else if(document.getElementById('pState').value=="MI"){
  61.                 document.getElementById('propertyState').value="Michigan";
  62.             } else if(document.getElementById('pState').value=="MN"){
  63.                 document.getElementById('propertyState').value="Minnesota";
  64.             } else if(document.getElementById('pState').value=="MS"){
  65.                 document.getElementById('propertyState').value="Mississippi";
  66.             } else if(document.getElementById('pState').value=="MO"){
  67.                 document.getElementById('propertyState').value="Missouri";
  68.             } else if(document.getElementById('pState').value=="MT"){
  69.                 document.getElementById('propertyState').value="Montana";
  70.             } else if(document.getElementById('pState').value=="NC"){
  71.                 document.getElementById('propertyState').value="North Carolina";
  72.             } else if(document.getElementById('pState').value=="ND"){
  73.                 document.getElementById('propertyState').value="North Dakota";
  74.             } else if(document.getElementById('pState').value=="NE"){
  75.                 document.getElementById('propertyState').value="Nebraska";
  76.             } else if(document.getElementById('pState').value=="NH"){
  77.                 document.getElementById('propertyState').value="New Hampshire";
  78.             } else if(document.getElementById('pState').value=="NJ"){
  79.                 document.getElementById('propertyState').value="New Jersey";
  80.             } else if(document.getElementById('pState').value=="NM"){
  81.                 document.getElementById('propertyState').value="New Mexico";
  82.             } else if(document.getElementById('pState').value=="NM"){
  83.                 document.getElementById('propertyState').value="New Mexico";
  84.             } else if(document.getElementById('pState').value=="NY"){
  85.                 document.getElementById('propertyState').value="New York";
  86.             } else if(document.getElementById('pState').value=="OH"){
  87.                 document.getElementById('propertyState').value="Ohio";
  88.             } else if(document.getElementById('pState').value=="OK"){
  89.                 document.getElementById('propertyState').value="Oklahoma";
  90.             } else if(document.getElementById('pState').value=="OR"){
  91.                 document.getElementById('propertyState').value="Oregon";
  92.             } else if(document.getElementById('pState').value=="PA"){
  93.                 document.getElementById('propertyState').value="Pennsylvania";
  94.             } else if(document.getElementById('pState').value=="RI"){
  95.                 document.getElementById('propertyState').value="Rhode Island";
  96.             } else if(document.getElementById('pState').value=="SC"){
  97.                 document.getElementById('propertyState').value="South Carolina";
  98.             } else if(document.getElementById('pState').value=="SD"){
  99.                 document.getElementById('propertyState').value="South Dakota";
  100.             } else if(document.getElementById('pState').value=="TN"){
  101.                 document.getElementById('propertyState').value="Tennessee";
  102.             } else if(document.getElementById('pState').value=="TX"){
  103.                 document.getElementById('propertyState').value="Texas";
  104.             } else if(document.getElementById('pState').value=="UT"){
  105.                 document.getElementById('propertyState').value="Utah";
  106.             } else if(document.getElementById('pState').value=="VT"){
  107.                 document.getElementById('propertyState').value="Vermont";
  108.             } else if(document.getElementById('pState').value=="VA"){
  109.                 document.getElementById('propertyState').value="Virginia";
  110.             } else if(document.getElementById('pState').value=="WA"){
  111.                 document.getElementById('propertyState').value="Washington";
  112.             } else if(document.getElementById('pState').value=="WV"){
  113.                 document.getElementById('propertyState').value="West Virginia";
  114.             } else if(document.getElementById('pState').value=="WI"){
  115.                 document.getElementById('propertyState').value="Wisconsin";
  116.             } else if(document.getElementById('pState').value=="WO"){
  117.                 document.getElementById('propertyState').value="Wyoming";
  118.             }
  119.             document.getElementById('tableCompletionFeedback').background="Images/completionBLUE3%.jpg";
  120.             document.getElementById('percent').innerHTML="3% complete";
  121.  
  122.             document.getElementById('propST').value=document.getElementById('pState').value;
  123.             document.getElementById('percent').style.fontFamily="Arial Narrow"; 
  124.             document.getElementById('percent').style.fontSize="10pt";
  125.             document.getElementById('percent').align="right";
  126.  
  127.             document.getElementById('tableRecap').style.visibility="visible";
  128.  
  129.             document.getElementById('cellOutput01').style.height="10";
  130.             document.getElementById('cellOutput02').style.height="10";
  131.             document.getElementById('cellOutput03').style.height="10";
  132.             document.getElementById('cellOutput04').style.height="10";
  133.             document.getElementById('cellOutput05').style.height="10";
  134.  
  135.             document.getElementById('cellOutput01').style.fontFamily="Arial Narrow"; 
  136.             document.getElementById('cellOutput01').style.fontSize="10pt";
  137.             document.getElementById('cellOutput01').innerHTML="Property is located in "+document.getElementById('propertyState').value;
  138.             document.screenOne.pState.style.backgroundColor="#FFFFFF";
  139.  
  140.             document.screenOne.nearWater1.disabled = false;
  141.             document.screenOne.nearWater2.disabled = false;
  142.  
  143.             document.getElementById('nearWater1').focus();
  144.         }
  145.  
  146.     if(x=="2"){
  147.             document.getElementById('tableCompletionFeedback').background="Images/completionBLUE6%.jpg";
  148.             document.getElementById('percent').innerHTML="6% complete";        
  149.             document.getElementById('nearWater').value=document.getElementById('nearH2O').value;
  150.  
  151.             document.getElementById('percent').style.fontFamily="Arial Narrow"; 
  152.             document.getElementById('percent').style.fontSize="10pt";
  153.             document.getElementById('percent').align="right";
  154.  
  155.             document.getElementById('cellOutput02').style.fontFamily="Arial Narrow"; 
  156.             document.getElementById('cellOutput02').style.fontSize="10pt";
  157.             var limit=document.screenOne.nearH2O.length;
  158.             for (i=0;i<limit;i++) {
  159.             if (document.screenOne.nearH2O[0].checked==true) { // NO
  160.                 status="REFERRED";
  161.                 document.getElementById('cellOutput02').innerHTML="Property is not located within 10 miles of an ocean, bay, or waterway";
  162.             } else if (document.screenOne.nearH2O[1].checked==true) { // YES
  163.                 document.getElementById('cellOutput02').innerHTML="Property is located within 10 miles of an ocean, bay or waterway";
  164.                 }
  165.             }
  166.             document.screenOne.mVacant.disabled = false;
  167.             document.screenOne.mVacant.style.border="1px solid #003300"; 
  168.             scroll(0,0);
  169.             document.screenOne.mVacant.focus();
  170.         }
  171.  
  172.     if(x=="3"){
  173.             document.getElementById('tableCompletionFeedback').background="Images/completionBLUE9%.jpg";
  174.             document.getElementById('percent').innerHTML="9% complete";             
  175.             document.getElementById('monthsVacant').value=document.getElementById('mVacant').value;
  176.             document.getElementById('cellOutput03').style.fontFamily="Arial Narrow"; 
  177.             document.getElementById('cellOutput03').style.fontSize="10pt";
  178.             document.getElementById('cellOutput03').innerHTML="Property has been vacant "+rtrim(document.getElementById('monthsVacant').value)+" months";
  179.             if(document.getElementById('mVacant').value > 24){status="REFERRED"}
  180.             document.getElementById('percent').style.fontFamily="Arial Narrow"; 
  181.             document.getElementById('percent').style.fontSize="10pt";
  182.             document.getElementById('percent').align="right";
  183.  
  184.             document.screenOne.mVacant.style.backgroundColor="#FFFFFF";
  185.             document.screenOne.bankrupt1.disabled = false;
  186.             document.screenOne.bankrupt2.disabled = false;
  187.             document.getElementById('bankrupt1').focus();
  188.         }
  189.  
  190.       if(x=="4"){
  191.             document.getElementById('tableCompletionFeedback').background="Images/completionBLUE12%.jpg";
  192.             document.getElementById('percent').innerHTML="12% complete";      
  193.             document.getElementById('bankruptcy').value=document.getElementById('bankrupt').value;
  194.             document.getElementById('percent').style.fontFamily="Arial Narrow"; 
  195.             document.getElementById('percent').style.fontSize="10pt";
  196.             document.getElementById('percent').align="right";
  197.  
  198.             document.getElementById('cellOutput04').style.fontFamily="Arial Narrow"; 
  199.             document.getElementById('cellOutput04').style.fontSize="10pt";
  200.  
  201.             var limit=document.screenOne.bankrupt.length;
  202.             for (i=0;i<limit;i++) {
  203.             if (document.screenOne.bankrupt[0].checked==true) { // NO
  204.                 status="REFERRED";
  205.                 document.getElementById('cellOutput04').innerHTML="Risk has never filed bankruptcy";
  206.             } else if (document.screenOne.bankrupt[1].checked==true) { // YES
  207.                 document.getElementById('cellOutput04').innerHTML="Risk has a previous bankruptcy";
  208.                 }
  209.             }
  210.             document.getElementById('cD1').disabled = false;
  211.             document.getElementById('cD2').disabled = false;
  212.             document.getElementById('cD3').disabled = false;
  213.  
  214.             document.getElementById('desiredCoverage').focus();
  215.         }
  216.  
  217.       if(x=="5"){
  218.             document.getElementById('tableCompletionFeedback').background="Images/completionBLUE15%.jpg";
  219.             document.getElementById('percent').innerHTML="15% complete";             
  220.             document.getElementById('percent').style.fontFamily="Arial Narrow"; 
  221.             document.getElementById('percent').style.fontSize="10pt";
  222.             document.getElementById('percent').align="right";
  223.  
  224.             document.getElementById('cellOutput05').style.fontFamily="Arial Narrow"; 
  225.             document.getElementById('cellOutput05').style.fontSize="10pt";
  226.             var limit=document.screenOne.desiredCoverage.length;
  227.             for (i=0;i<limit;i++) {
  228.                 if (document.screenOne.desiredCoverage[0].checked==true) { // Property ONLY
  229.                     document.getElementById('coverageDesired').value="property";
  230.                     document.getElementById('tablePropertyCoverage').style.visibility="visible";
  231.                     document.getElementById('cellOutput05').innerHTML="Applicant has requested property coverage ONLY";
  232.  
  233.                 } else if (document.screenOne.desiredCoverage[1].checked==true) { // Liability ONLY
  234.                     document.getElementById('coverageDesired').value="liability";
  235.                     document.getElementById('tableLiabilityCoverage').style.visibility="visible";
  236.                     document.getElementById('cellOutput05').innerHTML="Applicant has requested liability coverage ONLY";
  237.  
  238.                 } else if (document.screenOne.desiredCoverage[2].checked==true) { // both
  239.                     document.getElementById('coverageDesired').value="both";
  240.                     document.getElementById('tablePropertyCoverage').style.visibility="visible";
  241.                     document.getElementById('tableLiabilityCoverage').style.visibility="visible";
  242.                     document.getElementById('cellOutput05').innerHTML="Applicant has requested both Property and Liability coverage";
  243.                 }
  244.             }
  245.             document.screenOne.B1.disabled = false;  
  246.             scroll(0,0);
  247.             document.getElementById('B1').focus();
  248.         }
  249.  
Here's the code for the first "continue" button and the swapnode function.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript; version=1.5">
  2. Node.prototype.swapNode = function (node) {
  3.   var nextSibling = this.nextSibling;
  4.   var parentNode = this.parentNode;
  5.   node.parentNode.replaceChild(this, node);
  6.   parentNode.insertBefore(node, nextSibling);
  7.   }  
  8. </script>
  9.  
  10. if(x=="clear1"){
  11.   document.getElementById('tableContainer1').swapNode   document.getElementById('tableContainer0'));
  12. document.getElementById('tableContainer1').style.visibility="hidden";
  13. document.getElementById('tableContainer0').style.visibility="visible";
  14. document.getElementById('tableContainer0').style.visibility="visible";
  15. document.getElementById('insuredName').focus();
  16.             }
  17.  
And, here's the code for the first previous page button

Expand|Select|Wrap|Line Numbers
  1. function previousPage(x){// returns user to the previous page
  2.     if(x=='P1'){
  3.                 document.getElementById('tableContainer1').swapNode(document.getElementById('tableContainer0'));
  4.                 document.getElementById('tableContainer0').style.visibility="hidden";
  5.                 document.getElementById('tableContainer1').style.visibility="visible";
  6.                 if(document.getElementById('nearH2O').value=="NO"){
  7.                     document.screenOne.nearH2O[0].checked=true;
  8.                 } else if(document.getElementById('nearH2O').value!="NO"){
  9.                     document.screenOne.nearH2O[1].checked=true;
  10.                 }
  11.                 setTimeout("document.getElementById('pState').focus()",100);
  12.         }
  13.  
I know you didn't expect this much code; but the whole script section is 1658 lines.
Feb 10 '09 #3

acoder
Expert Mod 15k+
P: 16,027
I'm not sure why you're using swapNode here and why the radio buttons are reset, but the problem can be solved by getting the set of elements and then indexing them:
Expand|Select|Wrap|Line Numbers
  1. var nearH2Oval = document.getElementById('nearH2O').value;
  2. var nearH2O = document.screenOne.nearH2O;
  3. if (nearH2Oval == "NO") {
  4.     nearH2O[0].checked=true;
  5. } ...
Feb 11 '09 #4

Post your reply

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