Welcome, Guest
Username: Password: Remember me

TOPIC: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how

Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 5 months ago #87285

  • docl
  • docl's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
I have two questions of drop down list type. The answers for the second question depend on question one.
So far I have solved this by creating the first question and then for each answer to the first question I have created another question that is only shown (using relavance equation)if the corresponding answer in q1 is chosen. It works but creates a small challenge - being that Q2 is no longer just one question but multiple questions - which I have to correct manually afterwards when doing statistics since it should actually only be one question..
Actually in the end Q1 and Q2 could be just one common question, but the answer list would be very long and to make it easier to choose the correct answer Q2 is made as a subgroup of Q1..

An example of what I'm trying to accomplish:

"Overall question"
Where are you from?
(this could be a very long list of countries, so to make it easier for the user I would divide it in two questions)

Q1 then being
Continent?
--
Asia
Africa
Europe
etc..
--

Q2 should then be another dropdown list with countries dependent on the answer to Q1, but preferably still just one question..
Country?
---
if Q1=Asia; China
if Q1=Asia; Thailand
etc
if Q1=Europe; Denmark
if Q1=Europe; Germany
etc

my solution now is that Q2 is actually Q2,Q3,Q4 etc
Q2 if Q1=Asia
Q3 if Q1=Europe
etc.

Any ideas how to keep it as just two questions (mostly for statistical reasons)?
Last Edit: 1 year 5 months ago by docl.
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 5 months ago #87291

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 5843
  • Thank you received: 715
  • Karma: 222
There are a lot of solution.

I have already this one:
Add this in template.js:
/* Function to filter a select by another select 
   In the same page
   var qID : the number of question to filter 
   var filterqID : the number of question filtering
   Copyleft 2012 Denis Chenu
   licence GPL v3
*/
function selectFilterByCode(qID,filterqID){
  $(document).ready(function(){
    var idSelectFilter = $("#question"+qID).find("select").attr('id');
    $("#"+idSelectFilter).hide();
    var idSelectFiltering = $("#question"+filterqID).find("select").attr('id');
    if(typeof idSelectFilter === 'undefined' || typeof idSelectFiltering === 'undefined' )
    { 
      return false;
    }
    else
    {
      var idNewSelectFilter = 'select'+qID
      var NewSelectElement = "<select id='"+idNewSelectFilter+"'><option value=''>"+$("#"+idSelectFilter+" option[value='']:first").text()+"</option></select>";
      $("#"+idSelectFilter).after(NewSelectElement);
      $("#"+idNewSelectFilter).width($("#"+idSelectFilter).width());
      $("#"+idSelectFiltering).change(function(){
        $('#'+idSelectFilter).val('');
        $('#'+idNewSelectFilter).val('');
        var valuefilter=$(this).val();
        $('#'+idNewSelectFilter+' option').not(':first').remove();
        $('#'+idSelectFilter+' option').each(function(){
          if($(this).attr('value').indexOf(valuefilter)==0){
            $(this).clone().appendTo('#'+idNewSelectFilter);
          }
        });
      });
 
      $("#"+idNewSelectFilter).change(function(){
        $('#'+idSelectFilter).val($(this).val());
      });
 
      if($("#"+idSelectFiltering).val()!=''){
        var valuefilter=$("#"+idSelectFiltering).val();
        $('#'+idSelectFilter+' option').each(function(){
          if($(this).attr('value').indexOf(valuefilter)==0){
            $(this).clone().appendTo('#'+idNewSelectFilter);
          }
        });
        if($("#"+idSelectFilter).val()!=''){
           $('#'+idNewSelectFilter).val($("#"+idSelectFilter).val());
        }
      }
    }
  });
}

And use 2 question: the second question have string code starting like the first one.
In the second question, lauch the script:
<script type="text/javascript" charset="utf-8">
selectFilterByCode({QID},{FILTERQUEST});
</script>

Put a survey sample example and a complete js file
Demo: demonstration.sondages.pro/index.php?sid...85&newtest=Y&lang=fr
Denis
PS: can't upload js file : find it here: demonstration.sondages.pro/upload/templa...nade/selectfilter.js
Attachments:
Last Edit: 1 year 5 months ago by DenisChenu.
The administrator has disabled public write access.
The following user(s) said Thank You: docl, mfpereira

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 5 months ago #87335

  • docl
  • docl's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Perfect, works like a charm
Thanks
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 months ago #91572

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Silver Donor
  • Posts: 182
  • Thank you received: 5
  • Karma: 5
Looks like a simple solution, but I can;t get it to work.
I added the code to the template.js in "Copy of default" template.
And followed instructions to the letter.

The thing is that this is not the only - current -JavaScript problem I'm having. I have enabled the survey (all surveys) to read javascript by going to Global Settings->Security->Filter HTML for XSS and setting it to "No".

I tested that javascript is enabled by using the following in a third question:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
alert("Test!");
});
</script>

And I get the alert. What am doing wrong here? Any thoughts or suggestions would be much appreciated. In fact, I downloaded the sample survey you most graciously provided and it doesn't work for me either.
Thank you as always,
David
Last Edit: 1 year 2 months ago by dweisser.
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 months ago #91594

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Silver Donor
  • Posts: 182
  • Thank you received: 5
  • Karma: 5
And seven hours later....

I think I found the solution. Even though I updated the template.js I'm using for the survey with the function as defined in the previous posts, it was not *making it's way* to the survey.

When I pasted that function in its entirety along with the function call in the source of the Filtered question, that did the trick.

If anyone has thoughts on why the template.js did not adequately communicate the function to the survey questions, I'd sure be interested.

Thanks wonderful Limers!
David
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 weeks ago #94517

  • mfpereira
  • mfpereira's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Hi good morning.

I'm trying use your instructions to make se same question you use in the demo.

But I get a error.

I can´t download de file you post.

Can you send me the file .lss please?

Thanks
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 weeks ago #94518

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 5843
  • Thank you received: 715
  • Karma: 222
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 weeks ago #94521

  • mfpereira
  • mfpereira's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Sorry the ignorance. But when I click the link a new web page is open with a code.

How a put this in .lss format to import for limesurvey.

Thanks again
The administrator has disabled public write access.

Re: Two questions - both dropdown lists, answer list to Q2 depends on Q1 - how 1 year 2 weeks ago #94522

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 5843
  • Thank you received: 715
  • Karma: 222
Right click / save as or when the page is open : save.

Denis
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.146 seconds
Donation Image