CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Can function RETURN a value? (http://www.codingforums.com/showthread.php?t=73241)

tdavis 11-23-2005 07:43 PM

Can function RETURN a value?
 
I know the answer must be yes, but I am really having a hard time figuring this out. I have a simple script below, that calculates age (I know I need to do some more work). I want to redisplay the value returned from the function. It works OK, because the result displays correctly in the alert.

I know this is simple, but what am I doing wrong?

Thanks!
-tdavis

<script type="text/javascript">
function calcAge(returnAge) {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getYear() + 1900;

var returnAge = year - dob_year;

alert("Your age is: " + returnAge);

}
</script>


<body>

<form name="form1">

<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type=submit onclick="calcAge(returnAge)">
</p>

<p>
Your age is:
<script language="Javascript">
var returnAge;
document.write(returnAge);
</script>
</p>

boxxer03 11-23-2005 08:29 PM

I know this isn't your problem, but just by looking real quick I would change the line:
Code:

var year=time.getYear() + 1900;
to:
PHP Code:

var year=time.get[B]Full[/B]Year(); 

.getYear is outdated and with .getFullYear you don't need to add + 1900 at the end.

boxxer03 11-23-2005 08:30 PM

PHP Code:

var year=time.getFullYear(); 

that's better....

PhotoJoe47 11-23-2005 08:32 PM

Yes you can by using the keyword "return".

Replace this line:

var returnAge = year - dob_year;

With this:

return year - dob_year;

Now when you call the function you can do it like this:

age = calcAge(dob);

I hope this helps?

boxxer03 11-23-2005 08:35 PM

You could also change the part where it says 'Your Age is:' and put in a text box there and display the results there, so you don't have to use another jscript. You could set it to readonly and use the style="" attribute to remove the borders fromt he textbox so you'd never know it was there.

tdavis 11-23-2005 08:55 PM

That is exactly what I need to do, but I am struggling with this just a bit.
Would I eliminate the submit button and replace it with a text field?

boxxer03 11-23-2005 09:04 PM

No, just change this code. Cause you still need the submit button for the function to be called and work:
Code:

<p>
Your age is:
<script language="Javascript">
var returnAge;
document.write(returnAge);
</script>
</p>

try something like:

PHP Code:

<p>
Your age is: <input type="text" name="r_age" size="10" readonly style="border:0px solid black; background-color:transparent;">
</
p>
</
form

and then change the code in your script to display it:

Code:

<script type="text/javascript">
function calcAge(returnAge) {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getYear() + 1900;

var returnAge = year - dob_year;

document.form1.r_age.value=returnAge

alert("Your age is: " + returnAge);

}
</script>

You can change the size, or name, or whatever you want to in the textbox. I was just doin' that to show you what you could do.

tdavis 11-23-2005 09:16 PM

Thanks. But there is one little problem. The value is returned correctly, but displays and then disappears. Do I need to change the attributes in the script?

boxxer03 11-23-2005 09:21 PM

trying switching the alert and the document.form1.r_age.value=returnAge. put the alert before the doucument one.

Brandoe85 11-23-2005 09:22 PM

Couple things I see. You have a parameter in your function that you're never using, which also has the same name as a variable you're declaring in your function. I think if you put a span or div tag in your page and passed the form back to your function, you could set the innerHTML of your div to the calculated age. You might want to change the input type to a button instead of submit, as it will clear everything out after the button is clicked.

Good luck;

tdavis 11-23-2005 09:31 PM

OK. I changed the submit to a button, and the answer does not disappear. So everything works great now. Hopefully, I can use this in the real form.

Thanks to ALL very much for your help!
-tdavis

tdavis 11-23-2005 09:51 PM

Well, it sort of works. The age seems to be calculated properly, but does not populate the AGE form field until the second time I try and specify a date. I do not actually enter the date, I am using another function for that.

Is there an issue using two functions here?

<td><b><input class="input" type="text" name="the_date" size="18" maxlength="20" class="input" onFocus="hideAll();showAndFocus('dateDiv',document.date_form.the_month);calcAge()"></b></td>

The page can be seen here:
http://www.tdavisconsulting.com/kenerly

Place the cursor on date of birth, and a drop down displays to enter dob.

thanks again!
-tdavis

PhotoJoe47 11-23-2005 11:09 PM

Your orginal question was can a function return a value. I showed you how it was done using the keyword return. Now you take the variable that you use to store the dob that the user input for the field you have label "Birthday:*". Now when you want to display the age you can use:

document.write(calcAge(dob);

Try it.

PhotoJoe47 11-24-2005 12:45 AM

I also notice one other little detail you might want to look at.

With this bit of code:

var returnAge = year - dob_year;

if the current date is before the user's birthday the age will be off by a year to much. So you will need to check and see if the current date (month and day) is greater than the user's birthday (month & year)

boxxer03 11-24-2005 05:38 AM

can you post the whole new code you are you using now?


All times are GMT +1. The time now is 11:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.