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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    var year=time.getFullYear(); 
    that's better....
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    trying switching the alert and the document.form1.r_age.value=returnAge. put the alert before the doucument one.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #10
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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;

  • #11
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  • #12
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  • #13
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #14
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #15
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you post the whole new code you are you using now?
    I'm only telling you how I would do it, not how its supposed to be done. ;)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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