Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Use a return from one if statement in another if statement?

    Hello everyone,

    I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement.
    Basically another part of the function is dependent on what the user selects in the radio button.

    Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works):
    Code:
    <script type="text/javascript">
    function getSelectedRadio() {
    len = document.dates.dep.length
       // returns the array number of the selected radio button or -1 if no button is selected
       if (document.dates.dep[0]) { // if the button group is an array (one button is not an array)
          for (var i=0; i<len; i++) {
             if (document.dates.dep[i].checked) {
                return i
             }
          }
       } else {
          if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero
       }
       // if we get to this point, no radio button is selected
       return -1;
    } 
    
    function Calculate() {
    var i = getSelectedRadio();
       if (i == -1) {
          alert("Please select if Marine entered Delayed Entry Program");
       } else {
          if (document.dates.dep[i]) { 
             return document.dates.dep[i].value;      } 
    else { 
             return document.dates.dep.value;      }
       }
    
    if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && 
              document.dates.dayDEAF.value < 01 && first return from above) { 
    			document.dates.yearDEP.value = (document.dates.yearDEAF.value);
    			document.dates.monthDEP.value = (document.dates.monthDEAF.value);
    			document.dates.dayDEP.value = (document.dates.dayDEAF.value);
    			document.dates.yearPEBD.value = (document.dates.yearDEAF.value);
    			document.dates.monthPEBD.value = (document.dates.monthDEAF.value);
    			document.dates.dayPEBD.value = (document.dates.dayDEAF.value);
    } 
    else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && 
              document.dates.dayDEAF.value < 01 && second return from above) {
    			document.dates.yearPEBD.value = (document.dates.yearAFADBD.value);
    			document.dates.monthPEBD.value = (document.dates.monthAFADBD.value);
    			document.dates.dayPEBD.value = (document.dates.dayAFADBD.value);
    			document.dates.yearDEP.value = "N/A";
    			document.dates.monthDEP.value = "N/A";
    			document.dates.dayDEP.value = "N/A";
         } 
    } 
    </script>
    I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    you cannot return from an if statement, only from a function. i.e. you either split that function in two or you drop the return and assign the value to a variable.

    PHP Code:
    function getDateValue() 
    {
        var 
    getSelectedRadio();
        if (
    == -1) {
            
    alert("Please select if Marine entered Delayed Entry Program");
        } else {
            if (
    document.dates.dep[i]) { 
                return 
    document.dates.dep[i].value;
            } 
            else { 
                return 
    document.dates.dep.value;
            }
        }
    }

    // ...
    if (document.dates.yearDEAF.value 1985 && document.dates.monthDEAF.value 01 && document.dates.dayDEAF.value 01 && getDateValue()) { 
    or
    PHP Code:
        var dateval;
    // ...
        
    } else {
            if (
    document.dates.dep[i]) { 
                
    dateval document.dates.dep[i].value;
            } 
            else { 
                
    dateval document.dates.dep.value;
            }
        }

    if (
    document.dates.yearDEAF.value 1985 && document.dates.monthDEAF.value 01 && document.dates.dayDEAF.value 01 && dateval) { 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured it out, this is my new script
    Code:
    function getSelectedDep() {
    value = "No";
       // returns the array number of the selected radio button or -1 if no button is selected
       for (var i=0; i<document.dates.dep.length; i++) {
             if (document.dates.dep[i].checked) {
                value = document.dates.dep[i].value;
                break;}}
            return value;}
    
    function Compute() {
    depValue = getSelectedDep();
    
    if (depValue=="No") {
          alert("Please select if Marine entered Delayed Entry Program");
          return;}
    if (depValue=="DepYes" && document.dates.yearDEAF.value <= 1984 && document.dates.monthDEAF.value <= 12 && document.dates.dayDEAF.value <= 31)
    { 
    			document.dates.yearDEP.value = (document.dates.yearDEAF.value);
    			document.dates.monthDEP.value = (document.dates.monthDEAF.value);
    			document.dates.dayDEP.value = (document.dates.dayDEAF.value);
    			document.dates.yearPEBD.value = (document.dates.yearDEAF.value);
    			document.dates.monthPEBD.value = (document.dates.monthDEAF.value);
    			document.dates.dayPEBD.value = (document.dates.dayDEAF.value);
    }
    }
    Thanks for the help!
    Last edited by DeviousMethods; 04-19-2011 at 03:18 AM. Reason: SOLVED


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •