1

We have a simple age calculating script, that takes a date input (3 selects - day, month, year) and determine how old. It is triggered by an onChange assigned to the year select.

There are several date inputs scattered through the form, each one set up the same, other than the input names (day1 vs day 1 vs day 3, etc).

Currently we have simply duplicated the js code and manually changed the input variables ...

function Age1()
{
var oldDay = document.step1.day1.value;
var oldMonth = document.step1.month1.value;
var oldYear = document.step1.year1.value;
and more script....

function Age2()
{
var oldDay = document.step1.day2.value;
var oldMonth = document.step1.month2.value;
var oldYear = document.step1.year2.value;
and more script....

and so forth.

Ideally we would like to reuse one script, rather than hardcoding one for each instance. I have tried a bunch of ideas to no avail, but the ideal would be to trigger it via: onChange="Age(X);" and then have the js insert the proper variable:

function Age(varX)
{
var oldDay = document.step1.dayX.value;
var oldMonth = document.step1.monthX.value;
var oldYear = document.step1.yearX.value;
and so on ....

Any ideas for us ? Thanks in advance

1 Answer 1

1
function Age(varX)
{
    var oldDay = document.step1['day' + varX].value;
    var oldMonth = document.step1['month' + varX].value;
    var oldYear = document.step1['year' + varX].value;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, that worked beautifully. I had tried that route earlier ... but had left the "." between step1 and the ['day' + varX] bit. Not sure why it is different, since the "." is included after the ['day' + varX] bit.
@Tom: The reason this works is that all JavaScript property names are strings. You can write them using dotted notation and giving the property name as a token (obj.property), or you can write them using bracketed notation and giving the property name as a string (obj['property']). From the JavaScript perspective, these are exactly identical. (Some compressors/minifiers/packers may treat them differently, but they're the same to the language.) And of course, since the name is given as a string in bracketed notation, it can be a literal string or the result of an expression (as above).
@T.J. +1 couldn't of said it better myself.
Ok,Thanks for the explanation. I have had the html side of design down for a while, but the javascript aspect is new for me. Got lots to learn ;-)
@Tom: "Not sure why it is different, since the "." is included after the ['day' + varX] bit." The . after document.step1['day' + varX] means you're looking at a property (value) on the object referenced by the value of the property document.step1['day' + varX]. A dot before the ['day' + varX] bit like this: document.step1.['day' + varX] would be a syntax error, you've already started using dotted notation and so the next character would be part of the property name, and property name tokens can't have [ in them (even though property names can, but that's a different topic).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.