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

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months 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
4 years 11 months 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
4 years 11 months ago - 4 years 11 months 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: 4 years 11 months ago by tpartner. Reason: Added code language
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months ago #182945 by tpartner
The script looks good.

You can still manually add a language to the code block - I have edited your post accordingly.

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
4 years 11 months ago #182946 by krosser

tpartner wrote: The script looks good.

You can still manually add a language to the code block - I have edited your post accordingly.


Many thanks for checking it out! It is just I have had some strange behavior of conditioned questions not being displayed properly after adding this code. When the survey is active the conditions work, but when it is inactive, then they don't work in the preview.

What should be added to the block, for example, for JS?

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months ago #182947 by DenisChenu
There are no submit button in preview, then the script broke when you try to update an attribute

Add this
Code:
if(!$('#ls-button-submit').length != 1) {
return;
}
just before // Identify this question

I don't understand why Expression Manager broke if there are error here … Maybe limesurvey core can do somthing here : EM javascript work with (some) broken JS in question text.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
More
4 years 11 months ago #182948 by krosser

DenisChenu wrote: There are no submit button in preview, then the script broke when you try to update an attribute

Add this

Code:
if(!$('#ls-button-submit').length != 1) {
return;
}
just before // Identify this question

I don't understand why Expression Manager broke if there are error here … Maybe limesurvey core can do somthing here : EM javascript work with (some) broken JS in question text.



Perhaps I didn't add it in the right place, but it brakes the script.
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
if(!$('#ls-button-submit').length != 1) {
return;
}
    // 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>

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months ago #182949 by tpartner
Try wrapping the whole thing in an if() statement that looks for the submit button:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
    if($('#ls-button-submit').length > 0) {
      // 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>

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: krosser
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months ago #182950 by DenisChenu

krosser wrote: Perhaps I didn't add it in the right place, but it brakes the script.

Perhaps i made an error …

It's not ! it with …
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {
 
            if($('#ls-button-submit').length != 1) {
                return;
            }
    // 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>

Personnaly i prefer to quit when not needed than wrap inside a big if when needed. I think it's more clear

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: krosser
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 11 months ago #182956 by tpartner

Personnaly i prefer to quit when not needed than wrap inside a big if when needed. I think it's more clear

Yeah but I think that's a matter of personal preference. :)

There is very little difference in execution resources between the two methods, although mine should be marginally quicker as the code inside the IF() statement is not executed.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose