Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Javascript not working in most browsers

More
8 years 11 months ago #141151 by Andrea01
Hi,
maybe someone can be of help:

Question 1: How old are you (=numerical Input)
Question2 is a hidden question (=Radio list which should be prefilled depending on question 1.
Subquestions:
-Younger then 14 years
-14 - 19 years
-20 - 29 years
-30 - 39 years
-40 - 49 years
-50 - 59 years
-older than 59 years.

Here is the code:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var prev_ans = '{INSERTANS:332996X166X4102}';

if(prev_ans <= 13) {
$('#answer332996X166X36861').attr('checked', true);

}

if((prev_ans >= 14)&& (prev_ans <= 19)) {
$('#answer332996X166X36862').attr('checked', true);
}

if((prev_ans >= 20)&& (prev_ans <= 29)) {
$('#answer332996X166X36863').attr('checked', true);
}

if((prev_ans >= 30)&& (prev_ans <= 39)) {
$('#answer332996X166X36864').attr('checked', true);
}

if((prev_ans >= 40)&& (prev_ans <= 49)) {
$('#answer332996X166X36865').attr('checked', true);
}

if((prev_ans >= 50)&& (prev_ans <= 59)) {
$('#answer332996X166X36866').attr('checked', true);
}
else {
$('#answer332996X166X36867').attr('checked', true); }
;

$('#movenextbtn').click();

$('#question3686').hide();
})</script>
This code only works in Internet Explorer. The correct radion button gets checked depending on numerical input. In Firefox, Opera and Chrome always the last radio button (=older than 59 years) gets checked, regardless the numerical input.

Any idea?

Thanks
Andrea
The topic has been locked.
More
8 years 11 months ago #141156 by Deusdeorum
Mind attaching a small sample of your survey with those questions?

However, if you only want to store age divided in categories in your dataset you can simply use the expression manager and question type equation :)
The topic has been locked.
More
8 years 11 months ago #141174 by Andrea01
Problem solved: 'else if' instead of 'if' and it seems to work in all browsers
The topic has been locked.
More
8 years 11 months ago - 8 years 11 months ago #141180 by Ben_V

Deusdeorum wrote: However, if you only want to store age divided in categories in your dataset you can simply use the Expression Manager and question type equation


Yes you're right it's very easy using an equation like:
Code:
{if(Q1 <= 13, 'Younger then 14 years', if(Q1 < 20, '14 - 19 years',if(Q1 < 30, '20 - 29 years','older than 29 years')))}

On the other hand, you may have to use another question type if you want to display the repartition by age ranges in public statistics... As far as I know, it's not possible with equation question type (quiet logical if answers are strings)

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 8 years 11 months ago by Ben_V.
The topic has been locked.
More
8 years 11 months ago - 8 years 11 months ago #141220 by tpartner

On the other hand, you may have to use another question type if you want to display the repartition by age ranges in public statistics... As far as I know, it's not possible with equation question type (quiet logical if answers are strings)

You can:
1) use a list-radio type for the the age category question
2) place it in a following group
3) set it with a following equation type question

In a simplified example, with ages from 1-10 (Q1) and 3 age categories (Q2), the equation question would look like this:
Code:
{Q2=if(Q1 < 4, 'A1', if(Q1 < 8, 'A2', 'A3'))}

Sample survey attached:

File Attachment:

File Name: limesurvey...3(1).lss
File Size:18.21 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 11 months ago by tpartner.
The following user(s) said Thank You: Ben_V
The topic has been locked.
More
8 years 11 months ago #141223 by Ben_V
Thank you Tony for those trick and sample,...
I'm glad to learn that this approach is fully compatible with radio type questions. :)
(for users needing something similar using numerical input question, there is an old discussion here )

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The topic has been locked.
More
8 years 11 months ago #141231 by Andrea01
Thanks a lot Tony. I changed Q1 into a numerical Input question and it also worked :) :)
The topic has been locked.
More
8 years 11 months ago #141243 by Andrea01
I just noticed that the script only works when the survey Format it "Group by Group". Is there a way when survey is shown "question by question"?
The topic has been locked.
More
8 years 11 months ago #141244 by tpartner
I believe you can only set a list-radio with an equation question if they are both on the same page.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Andrea01
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose