Quantcast
Channel: HostGator Peer Support Forums
Viewing all articles
Browse latest Browse all 12024

Javascript headaches

$
0
0
Its another late weekend and I am pounding my head against the wall. What should be easy is drawing a complete blank.

I have a simple age calculating script for use in a form. It works just fine. But there are several spots on the form that it will be used. Everything is already coded with variables (ie: month1, month 2, month 3 ect) for each instance.

What I am trying to do without success is modify the javascript so that the variable can be fed by the onChange event. As an example: onChange="Age(2);" would cause the script to function based on day2, month2, year2 and age 2, onChange="Age(4);" would work with day4, month4, etc. If all else fails I could always write multiple instances of the js for each set of date inputs, but am hoping to keep the code smaller.

The script is below (step1 is the form name, day1, month1, year1, age1 the input names for the first instance):
Code:

<script language="javascript">
<!--
function Age()
{
    var birthDay = document.step1.day1.value;
    var birthMonth = document.step1.month1.value;
    var birthYear = document.step1.year1.value;
    var userAge;
    var currentDay = new Date();
    var dayNow = currentDay.getDate();
    var monthNow = (currentDay.getMonth()+1);
    var yearNow = currentDay.getFullYear();

    if((monthNow > birthMonth)||(monthNow == birthMonth & dayNow >= birthDay))
    { userAge = yearNow - birthYear; }
    else { userAge = yearNow - birthYear - 1; }

    document.step1.age1.value = userAge;
}
//-->
</script>


Viewing all articles
Browse latest Browse all 12024

Trending Articles