Bienvenue, Invité
Nom d'utilisateur : Mot de passe : Se souvenir de moi

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

calculate age from birthdate in CPDB token as answer to hidden question il y a 11 mois 2 semaines #99834

  • seancon1
  • Portrait de seancon1
  • Hors ligne
  • Fresh Lemon
  • Messages : 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
L'administrateur a désactivé l'accès en écriture pour le public.

calculate age from birthdate in CPDB token as answer to hidden question il y a 11 mois 2 semaines #99845

  • seancon1
  • Portrait de seancon1
  • Hors ligne
  • Fresh Lemon
  • Messages : 3
  • Karma: 0
this was supposed to be in the Can i do this with limesurvey? section.
L'administrateur a désactivé l'accès en écriture pour le public.

calculate age from birthdate in CPDB token as answer to hidden question il y a 11 mois 2 semaines #99869

  • seancon1
  • Portrait de seancon1
  • Hors ligne
  • Fresh Lemon
  • Messages : 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
L'administrateur a désactivé l'accès en écriture pour le public.

calculate age from birthdate in CPDB token as answer to hidden question il y a 11 mois 2 semaines #99875

  • Ben_V
  • Portrait de Ben_V
  • Hors ligne
  • Platinum Lime
  • Messages : 1068
  • Remerciements reçus 235
  • Karma: 74
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...
L'administrateur a désactivé l'accès en écriture pour le public.
Modérateurs: ITEd
Temps de génération de la page : 0.789 secondes
Donation Image