- Posts: 75
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Javascript not working in most browsers
- Andrea01
- Topic Author
- Offline
- Senior Member
Less
More
7 years 6 months ago #141151
by Andrea01
Javascript not working in most browsers was created 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
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.
- Deusdeorum
- Offline
- Senior Member
Less
More
- Posts: 72
- Thank you received: 17
7 years 6 months ago #141156
by Deusdeorum
Replied by Deusdeorum on topic Javascript not working in most browsers
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
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.
- Andrea01
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 75
- Thank you received: 0
7 years 6 months ago #141174
by Andrea01
Replied by Andrea01 on topic Javascript not working in most browsers
Problem solved: 'else if' instead of 'if' and it seems to work in all browsers
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1223
- Thank you received: 351
7 years 6 months ago - 7 years 6 months ago #141180
by Ben_V
Yes you're right it's very easy using an equation like:
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)
Replied by Ben_V on topic Javascript not working in most browsers
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: 7 years 6 months ago by Ben_V.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3690
7 years 6 months ago - 7 years 6 months ago #141220
by tpartner
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:
Sample survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Javascript not working in most browsers
You can: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)
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:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 7 years 6 months ago by tpartner.
The following user(s) said Thank You: Ben_V
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1223
- Thank you received: 351
7 years 6 months ago #141223
by Ben_V
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)
Replied by Ben_V on topic Javascript not working in most browsers
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 )
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.
- Andrea01
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 75
- Thank you received: 0
7 years 6 months ago #141231
by Andrea01
Replied by Andrea01 on topic Javascript not working in most browsers
Thanks a lot Tony. I changed Q1 into a numerical Input question and it also worked
The topic has been locked.
- Andrea01
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 75
- Thank you received: 0
7 years 6 months ago #141243
by Andrea01
Replied by Andrea01 on topic Javascript not working in most browsers
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.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3690
7 years 6 months ago #141244
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Javascript not working in most browsers
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.