Welcome, Guest
Username: Password: Remember me

TOPIC: calculate age from birthdate in CPDB token as answer to hidden question

calculate age from birthdate in CPDB token as answer to hidden question 1 year 2 weeks ago #99834

  • seancon1
  • seancon1's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
we import the participants birthday to the Central Participants Database, we want to add the age to the answers so we can create reports against the age...

i have created this javascript and it works correctly until i try to add the token as a variable
<script type="text/javascript" charset="utf-8">
 
   $(document).ready(function() {
 
    function calculateAge(birthDate) {
    birthDate = new Date(birthDate);
    var otherDate = new Date();
    var years = (otherDate.getFullYear() - birthDate.getFullYear());
    if (otherDate.getMonth() < birthDate.getMonth() || otherDate.getMonth() == 
           birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
        years--;
   }
    return years;
}
var dob = '{TOKEN:ATTRIBUTE_CPDB_2}';                      
var age = calculateAge(dob);
 
//  document.getElementById('q1124').value= age;
 
alert(age);
 
   });
 
</script>

above i have it modified to alert the age but the commented line is the desired result.

i do not get an alert unless i swap the token for an actual date '11/01/1970' then the alert shows the age.

if i look at the source i see this
var dob = '<span style='background-color: #eee8aa;'><span title='Undefined variable' style='border-style: solid; border-width: 2px; border-color: red;'><span title='Undefined variable' style='color: red; font-weight: bold'>TOKEN:ATTRIBUTE_CPDB_2</span></span></span>';     


not sure what i am doing wrong here

Sean
The administrator has disabled public write access.

calculate age from birthdate in CPDB token as answer to hidden question 1 year 2 weeks ago #99845

  • seancon1
  • seancon1's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
this was supposed to be in the Can i do this with limesurvey? section.
The administrator has disabled public write access.

calculate age from birthdate in CPDB token as answer to hidden question 1 year 2 weeks ago #99869

  • seancon1
  • seancon1's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
i was able to fix the alert by changing
var dob = '{TOKEN:ATTRIBUTE_CPDB_2}';

to
var dob = "{TOKEN:ATTRIBUTE_CPDB_2}";

i get the correct age in the alert now.


to complete this i would like to change the alert
alert(age);

to enter the age as a default answer to the question.

is this done in the java code or by inserting a variable in the default answer field?

i have tried the following
$('#answer464957X16X491 input.text').val(age);

and
$('#answer464957X16X491').val(age);

as well as {age} as a default answer

none of the above worked

how can i accomplish this?


Sean
The administrator has disabled public write access.

calculate age from birthdate in CPDB token as answer to hidden question 1 year 1 week ago #99875

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 1098
  • Thank you received: 247
  • Karma: 78
After double checking SGQA code ;) maybe try :
document.getElementById("#answer464957X16X491").value = (age);

BTW, I suppose you can be interested in this old post
Benoît

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.516 seconds
Donation Image