Welcome to the LimeSurvey Community Forum

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

Make "Submit" button appear only if people answer "Yes" for a Yes/No question

More
6 years 4 months ago #160957 by FaLifeTime
Hi,

i tried the script and works perfectly. My situation is that i'm trying to only condition the submit button if a particular YES/NO question "shows up". If its hidden or not, with this code it always gonna wait for the user to response the YES/NO question.

Is there a way to only make this code works if the question is visible?

Thanks and excuses me for my english,
4aLifeTime
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #160962 by tpartner
How is the question hidden? By relevance on this page or by something on previous pages?

If on previous pages, this should work:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Initially disable the Next/Submit button
    if($(thisQuestion).is(':visible')) {
      $('#movenextbtn, #movesubmitbtn').prop('disabled', 'true').addClass('ui-state-disabled');
    }
 
    // Listeners on Yes/No radios to toggle the Next/Submit button
    $('input.radio[value="Y"], label[id$="X{QID}Y"]', thisQuestion).click(function(){
      $('#movenextbtn, #movesubmitbtn').prop('disabled', '').removeClass('ui-state-disabled');
    });
    $('input.radio[value="N"], label[id$="X{QID}N"]', thisQuestion).click(function(){
      $('#movenextbtn, #movesubmitbtn').prop('disabled', 'true').addClass('ui-state-disabled');
    });
 
    });
</script>

If dynamically on this page, you will need listeners on whatever questions are showing/hiding this question.

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: socius, FaLifeTime
The topic has been locked.
More
6 years 4 months ago #160983 by FaLifeTime
Exactly what i needed. Thanks a lot!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #160996 by Joffm
Hi, all,
to be honest, I do not understand this question.

Why should anybody insert a question which offers two answer options, but one without any function?

I think of a scenario like "You have to agree to our terms to proceed...."
In this case people are used to tick a checkbox .

So why not just use a mandatory multiple question with one subquestion.

This should cover everything needed.

Except the challenge for Tony to find a way and develop a new script;)


Maybe somebody can explain to me why this approach with "Yes/No", but without possibility to click "No", was the only way.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu, socius
The topic has been locked.
More
6 years 3 months ago #161958 by socius
Hi all,

thanks to @tpartner for the work and reworks of the script!
I also see your point @Joffm - in the case of a single Y/N I think an error message could suffice.

In my case I have long arrays with lots of statements and agree/disagree scales.
To make answering easier and not frighten my respondents seeing all the lines at once, I would show the array linewise and therefore use subquestion relevance and only show the n+1-th question when the n-th question was answered, etc. This works great with

Question: q1
Subquestions:
sq1 sq_relevance: empty
sq2 sq_relevance: !is_empty(q1_sq1)
sq3 sq_relevance: !is_empty(q1_sq2)
etc.

But: I do not want the "Next" Button to be hidden until the last question of the array was answered. When it unhides/appears it's clear to the respondents that they can continue.

My question is, how your script below (I use LS 2.06LTS) can be reworked to be used for that, i.e. to hide the "Next" Button until the last question of the array was answered (no matter how many questions there are in this array).

Thanks a lot for your help on this!
Best, G


Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Initially disable the Next/Submit button
    $('#movenextbtn, #movesubmitbtn').prop('disabled', 'true').addClass('ui-state-disabled');
 
    // Listeners on Yes/No radios to toggle the Next/Submit button
    $('input.radio[value="Y"]', thisQuestion).click(function(){
      $('#movenextbtn, #movesubmitbtn').prop('disabled', '').removeClass('ui-state-disabled');
    });
    $('input.radio[value="N"]', thisQuestion).click(function(){
      $('#movenextbtn, #movesubmitbtn').prop('disabled', 'true').addClass('ui-state-disabled');
    });
 
    });
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 3 months ago - 6 years 3 months ago #161964 by tpartner
This will disable the Next/Submit button until all rows in an array are answered.

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Initially disable the Next/Submit button
    $('#movenextbtn, #movesubmitbtn').prop('disabled', 'true').addClass('ui-state-disabled');
 
    // Listeners on radios to enable the Next/Submit button
    $('input.radio', thisQuestion).click(function(){
      if($('input.radio:checked', thisQuestion).length == $('tr.answers-list', thisQuestion).length) {
        $('#movenextbtn, #movesubmitbtn').prop('disabled', '').removeClass('ui-state-disabled');
      }
    });
    });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 6 years 3 months ago by tpartner.
The following user(s) said Thank You: socius
The topic has been locked.
More
6 years 3 months ago #162024 by socius
Thank you Tony! That works perfectly fine!
Best, G
The topic has been locked.
More
5 years 6 months ago #174840 by Matiasko
I would like to revoke this topic since version 3.0 is here for some time, already. I would like to know how should the code look like right now?

I'm interested in disabling the Submit (or Next) button if:
- Yes option was selected in Yes/No question
- Certain/First option was selected in radio single answer question
- Certain/First option was selected in multiple answer question

Button should be disabled by default or when someone reselects other than the mentioned options.

I really like how the 'disabled' button looks like instead of hiding it, so please keep it that way in code :)

Regards

Matt
The topic has been locked.
More
5 years 2 weeks ago #182687 by terryaulenbach
I would also like to know how to make this work with 3.14 for a simple Yes/No question. I have been fighting with it to no avail. I'm not sure what has changed, but I just can't get it to work. Help?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 2 weeks ago #182689 by tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
5 years 2 weeks ago #182694 by terryaulenbach
Thank you as always, Tony. I have been searching and searching for this but that article never materialised for me. I owe you a virtual beer.
The topic has been locked.
More
5 years 1 week ago - 5 years 1 week ago #182942 by krosser
I have tried to write a script for such a case, although the Submit button is simply disabled, not hidden.
I would appreciate if you guys can check it out because I am not sure it is bugless.
The code is placed into a radio list type of question with 'yes' and 'no' answer choices, coded as A1 and A2.
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Initially disable the Next/Submit button
    $('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
 
    // Listeners on Yes/No radios to toggle the Next/Submit button
    $('input:radio[value="A1"]', thisQuestion).click(function(){
      $('#ls-button-submit').prop('disabled', '').removeClass('ui-state-disabled');
    });
    $('input:radio[value="A2"]', thisQuestion).click(function(){
      $('#ls-button-submit').prop('disabled', 'true').addClass('ui-state-disabled');
    });
    });
</script>

Thanks in advance! A survey sample attached.

File Attachment:

File Name: limesurvey...4351.lss
File Size:17 KB


PS. Does anyone know why we no longer can select in which language the code is displayed on the forum? It is only gray now.

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
Last edit: 5 years 1 week ago by tpartner. Reason: Added code language
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose