This script allows you to ask for a participant's year of birth in a Single Choice question. Using the provided year of birth value, the script jumps to the relevant calendar year in a Date question, and populates the age value in a Number question.
Note: The age value is static and captured at the time the script executes. It will not change over time.
Detailed Steps
- In your survey, create a new Page element.
- Add a Single Choice question to the Page element.
- Set Display answers as to Dropdown.
- Add your answers starting from the lowest year to the highest.
Tip: Instead of manually entering all these values, you can quickly generate the list of birth years in Excel and then add answers in batch. To do this:
- In an Excel column, type the first few values in the birth year range in ascending order (for example, 1920, 1921, 1922).
- Select and highlight the values you typed.
- Hover your cursor over the tiny square in the bottom right corner of the selected range until it becomes a crosshair.
- Hold your left mouse click down and slowly drag the range downward, extending it down the length of the column.
Excel automatically detects the increasing count logic and populates the cells. In the example screenshot below, the provided values stop at cell A3 with 1922. Excel will auto-fill cell A4 with 1923, A5 with 1924, and so on.
- Keep extending the range down the column until you reach your desired end year (for example, 2020).
- Copy the entire range of birth year values.
- In the Single Choice question, in the Answers area, click the options menu and select Manage Answers in Batch.
- In the Manage Answers in Batch dialog, paste the range of birth year values and click OK.
- Add a second Page element.
- Add a Date question and a Number question to the second Page element.
You may want to temporarily leave these unhidden for testing. - Open the first Page element for editing and click the Scripting button.
- Copy and paste the script below into the On Complete tab.
- Replace YOB with the Single Choice question name.
- Replace AgeCalc with the Number question name.
- Replace DOB with the Date question name.
- Update min_Year and max_Year with the same options as your lowest and highest options from your Single Choice question.
For example, min_Year could be 1920 and max_Year 2020. - Click Save.
- Preview and test your survey.
Script
/* Script Name: How do I use the birth year response from a Single Choice question
to set the calendar in a Date question and calculate age? (Modern Surveys) To ensure your script works as expected, always copy the original script
from the Script Library found in the Alida Help Center. Failure to use the
latest script from the script library may cause unexpected results. */ const year = response.getDataPoint("YOB");
const ageCalc = response.getDataPoint("AgeCalc");
const dob = response.getDataPoint("DOB");
const minYear = min_Year;
const maxYear = max_Year;
// Get the entered data
var y = year.getSelectedIndex();
var tYear = 0;
for (var index = 0; index <= maxYear - minYear; index++) {
if(index == y) {
tYear = minYear + y ;
break;
}
}
// New date object on the 1st of the given year and month (month is zero based)
var date = new Date(tYear, 0, 1);
// Calculate age
var diff_ms = Date.now() - date.getTime();
var age_dt = new Date(diff_ms);
var age = age_dt.getFullYear() - 1970;
//Set date and DOB from January 1st and entered year
ageCalc.set(age);
dob.set(date);
Example
First page with Single Choice year of birth question:
Second page with Date question showing the calendar year for participant's year of birth, and Number question showing the auto-calculated age:
Comments
0 comments
Article is closed for comments.