Welcome to the LimeSurvey Community Forum

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

Multiple choice and block selecting more then max

  • Georeatri_106344
  • Georeatri_106344's Avatar
  • Visitor
  • Visitor
5 years 6 months ago - 5 years 6 months ago #173274 by Georeatri_106344
Replied by Georeatri_106344 on topic Multiple choice and block selecting more then max
Hi,

I was wondering if someone knows an update for the code in Limesurvey 3. I can't seem to get it to work.

I figured the following part doesn't work:
Code:
$('input.checkbox', thisQuestion).change(function(e) {
      checkMax();
    });

To test, I've added an alert:
Code:
$('input.checkbox', thisQuestion).change(function(e) {
alert('hello');
      checkMax();
    });
Whenever i enable or disable a checkbox there's no alert.
TO make sure javascript works i added the alert at the beginning, and that shows me an alert when the page loads:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 alert('hello');
    // Maximun answers
    var maxAnswers = 3;
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    function checkMax() {
      $('input.checkbox', thisQuestion).prop('disabled', false);
      if($('input.checkbox:checked', thisQuestion).length >= maxAnswers) {
        $('input.checkbox', thisQuestion).not(':checked').prop('disabled', true);
      }
    }
 
    // Initial checkbox states
    checkMax();
 
    // Listener on the checkboxes
    $('input.checkbox', thisQuestion).change(function(e) {
      checkMax();
    });
 
    // Remove any "disabled" properties before submitting
    $('#movenextbtn, #movesubmitbtn').bind('click', function () {      
      $('input.checkbox', thisQuestion).prop('disabled', false);
    });
 
    });
</script>
Last edit: 5 years 6 months ago by Georeatri_106344.
The topic has been locked.
More
5 years 6 months ago #173277 by jelo
In LS3 the old workarounds no longer work.

E.g.the format of 'input.checkbox' is now 'input [type="checkbox"]'.
Take a look at some other threads, where old scripts were rewritten.
Perhaps you can get your script fixed with these example.

www.limesurvey.org/forum/can-i-do-this-w...oxes?start=15#168095

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • Georeatri_106344
  • Georeatri_106344's Avatar
  • Visitor
  • Visitor
5 years 6 months ago #173286 by Georeatri_106344
Replied by Georeatri_106344 on topic Multiple choice and block selecting more then max
Thanks for the tip. I managed to get it to work, also with thanks to the jquery documentation. I changed the function checkmax:
Code:
function checkMax() {
      $('input[type="checkbox"]', thisQuestion).prop('disabled', false);
      if($("input:checked", thisQuestion).length >= maxAnswers) {
        $('input[type="checkbox"]', thisQuestion).not(':checked').prop('disabled', true);
      }
    }
The topic has been locked.
  • Georeatri_106344
  • Georeatri_106344's Avatar
  • Visitor
  • Visitor
5 years 6 months ago #173290 by Georeatri_106344
Replied by Georeatri_106344 on topic Multiple choice and block selecting more then max
It was late last night, so I forgot to mention I changed more then just the checkMax function in order to get it to work under LimeSurvey 3. In case anyone uses the search, this is the full code:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Maximun answers
    var maxAnswers = 2;
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    function checkMax() {
      $('input[type="checkbox"]', thisQuestion).prop('disabled', false);
      if($("input:checked", thisQuestion).length >= maxAnswers) {
        $('input[type="checkbox"]', thisQuestion).not(':checked').prop('disabled', true);
      }
    }
 
    // Initial checkbox states
    checkMax();
 
    // Listener on the checkboxes
    $('input[type="checkbox"]', thisQuestion).on('change', function(e) {
      checkMax();
    });
 
    // Remove any "disabled" properties before submitting
    $('#movenextbtn, #movesubmitbtn').bind('click', function () {
      $('input[type="checkbox"]', thisQuestion).prop('disabled', false);
    });
 
  });
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 6 months ago - 5 years 6 months ago #173292 by tpartner
I think you will also want to change the submit listener as there is no #movenextbtn element anymore.

Maybe this...

Code:
$('#limesurvey').on('submit', function(e) {

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 6 months ago by tpartner.
The topic has been locked.
More
5 years 6 months ago #173293 by jelo

tpartner wrote: I think you will also want to change the submit listener as there is no #movenextbtn element anymore.

Isn't that a back-step in development? No easy way to check if back or next were clicked, right?

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 6 months ago #173294 by tpartner
Yes, you could target the buttons but I find that putting a listener on the form submit event also gets fired with the "Save and resume" function so data is always saved. Of course, it should be tested in individual workarounds (I am on my phone today so can't test).

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 6 months ago #173298 by jelo

tpartner wrote: Yes, you could target the buttons

Then I got you wrong, I got the impression that it would be no longer possible in LS3.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose