Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-17-2011, 05:55 AM   PM User | #1
DeviousMethods
New to the CF scene

 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
DeviousMethods is an unknown quantity at this point
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!
DeviousMethods is offline   Reply With Quote
Old 04-17-2011, 10:16 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,881
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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) { 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 04-17-2011, 12:22 PM   PM User | #3
DeviousMethods
New to the CF scene

 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
DeviousMethods is an unknown quantity at this point
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
DeviousMethods is offline   Reply With Quote
Reply

Bookmarks

Tags
if statement, javascript, radio button, return

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:45 AM.


Advertisement
Log in to turn off these ads.