- Posts: 10
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Conditionnal dropdown lists
- La_civette
- Topic Author
- Offline
- New Member
Less
More
7 years 3 months ago #146942
by La_civette
Conditionnal dropdown lists was created by La_civette
Hi folks,
Using LS for a while but always pushing it further according my needs. I really love the toolset.
I came to a double issue recently. I search the forum without finding any sure clue.
1. Is it possible to have a drop down list modified on the fly according a previous YES/No question in same group ?
If yes, options are 0,1,2,3,4,5,6,7,8,9
If no, options are 0,1
2. Is it possible to have 2 dropdown lists conditioned by each other list in same group
If Q1 is 1 then Q2 has to be 0
If Q2 is 1 then Q1 has to be 0
I believe solution is at javascript level but am not sure.
Thank you beforehand,
Using LS for a while but always pushing it further according my needs. I really love the toolset.
I came to a double issue recently. I search the forum without finding any sure clue.
1. Is it possible to have a drop down list modified on the fly according a previous YES/No question in same group ?
If yes, options are 0,1,2,3,4,5,6,7,8,9
If no, options are 0,1
2. Is it possible to have 2 dropdown lists conditioned by each other list in same group
If Q1 is 1 then Q2 has to be 0
If Q2 is 1 then Q1 has to be 0
I believe solution is at javascript level but am not sure.
Thank you beforehand,
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
7 years 3 months ago - 7 years 3 months ago #146949
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 Conditionnal dropdown lists
1) The simplest solution is to have 2 versions of the drop-down question. Show the appropriate question via relevance based on the Yes/No question.
2) There is no question validation equation for drop-downs so you'll have to write your own validation in JavaScript. Something like this in the source of one of the questions:
2) There is no question validation equation for drop-downs so you'll have to write your own validation in JavaScript. Something like this in the source of one of the questions:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var q1Dropdown = $('select[id$="X{Q1.qid}"]'); var q2Dropdown = $('select[id$="X{Q2.qid}"]'); $('#movenextbtn, #movesubmitbtn').click(function(e) { var alertText; var moveAllowed = true; if(q1Dropdown.val() == 1 && q2Dropdown.val() != 0) { alertText = 'If Q1 is 1 then Q2 has to be 0'; moveAllowed = false; } if(q2Dropdown.val() == 1 && q1Dropdown.val() != 0) { alertText = 'If Q2 is 1 then Q1 has to be 0'; moveAllowed = false; } if(moveAllowed == false) { e.preventDefault(); alert(alertText); return false; } }); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 3 months ago by tpartner.
The topic has been locked.
- La_civette
- Topic Author
- Offline
- New Member
Less
More
- Posts: 10
- Thank you received: 1
7 years 3 months ago #146988
by La_civette
Replied by La_civette on topic Conditionnal dropdown lists
Indeed, forking the conditional flow is an easy way to get to the goal. Nevertheless it becomes problematic for a further question in group depending on EM calculation. If "hidden" question are not on the page it cannot calculate. Anyway I "workarounded" this as first scenario is a calculation and second one is always equal 0. I thus consider that empty string equals 0.
For the javascript I'll test this when I have a little time. In my case it would be a nice to have and not a must.
Thank you
For the javascript I'll test this when I have a little time. In my case it would be a nice to have and not a must.
Thank you
The topic has been locked.
- La_civette
- Topic Author
- Offline
- New Member
Less
More
- Posts: 10
- Thank you received: 1
7 years 3 months ago - 7 years 3 months ago #147089
by La_civette
Replied by La_civette on topic Conditionnal dropdown lists
For info this successfully works for me :
Thank you,
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var q1Dropdown = $('#answer247846X2X29'); var q2Dropdown = $('#answer247846X2X30'); $("#answer247846X2X29").change(function() { if(q1Dropdown.val() == '1' && q2Dropdown.val() != '0') { $("#answer247846X2X30").val('0'); } }); $("#answer247846X2X30").change(function() { if(q2Dropdown.val() == '1' && q1Dropdown.val() != '0') { $("#answer247846X2X29").val('0'); } }); }); </script>
Thank you,
Last edit: 7 years 3 months ago by La_civette.
The topic has been locked.