Welcome to the LimeSurvey Community Forum

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

Equal sum value does not render the question unacceptable

  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #114766 by slaugh
Hi!

Is it possible to set a "equal sum value" but to make it so that it doesn't make the question unacceptable if the participant doesn't respect the set value?

I'd like it to simply warn the participant when his answer is not logical, but not to prohibit him to go to the next page.

I am using LS 2.05+ and the default template.

Here is an active survey with the template I am using and a multiple numerical question with @equal sum value" activated : survey.cirano.qc.ca/index.php/552928/lang-en

Thank you!

Sophie
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago - 9 years 3 months ago #114795 by tpartner
To do that you would need to disable the LimeSurvey "Equals sum value" setting and use JavaScript to insert and handle the "totals" elements after page load.

Add something like this to the source of the question:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Insert the "totals" elements
    var insertedEl = '<li class="multiplenumerichelp help-item">\
            <span class="label">Remaining: </span>\
            <span class="dynamic_remaining">\
              <span>100</span>\
            %</span>\
          </li>\
          <li class="multiplenumerichelp  help-item">\
            <span class="label">Total: </span>\
            <span class="dynamic_sum">\
              <span>0</span>\
            %</span>\
          </li>';
    $('ul.subquestions-list', thisQuestion).append(insertedEl);
 
    // A listener on the inputs
    $('input[type=text]', thisQuestion).bind('change keyup', function(e) {
      handleInputs(thisQuestion);
    });
    $('input[type=text]', thisQuestion).bind('paste', function(e) {
      setTimeout(function () {
        handleInputs(thisQuestion);
      }, 100);
    });
 
    // Initialize sum values
    handleInputs(thisQuestion);
 
  });
 
  // A function to calculate totals in a multiple numeric
  function handleInputs(question) {
    var thisQuestion = $(question);
    var answered = 0;
    var sumTotal = 0;
    $('input[type=text]', thisQuestion).each(function(i) {
      sumTotal = Number(sumTotal) + Number($(this).val());
      if($(this).val() != '') {
        answered = Number(answered) + 1;
      }
    });
    $('span.dynamic_remaining span', thisQuestion).text(100 - sumTotal);
    $('span.dynamic_sum span', thisQuestion).text(sumTotal);
    $('span.dynamic_sum', thisQuestion).removeClass('good error');
    if(answered > 0 &amp;&amp; sumTotal == 100) {
      $('span.dynamic_sum', thisQuestion).addClass('good');
    }
    else if(answered > 0) {
      $('span.dynamic_sum', thisQuestion).addClass('error');
    }
  }
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 3 months ago by tpartner.
The following user(s) said Thank You: slaugh
The topic has been locked.
  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #114797 by slaugh
Thank you so much. It seems to work perfectly!

I'd like tot do the same thing with a question where the "equal_sum_value" is a number given by the participant in a previous question.

I guess the coding would be different.

See here for an exemple in an active survey: survey.cirano.qc.ca/index.php/481326/lang-fr

It is the third question (PROVSAL: QID13923) I am concerned about. It appears only if the total of the first question is greater than 0. And the total in third question should be equal to the first question total.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago - 9 years 3 months ago #114808 by tpartner
Well, for this specific case, you could add a listener on "number of employees" question as a control.

Something like this in the source of the "where are they from" question:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the elements
    var thisQuestion = $('#question{QID}');
    var controlQuestion = thisQuestion.prevAll('.numeric-multi:eq(0)');
    var inputs = $('input[type=text]', thisQuestion).add('input[type=text]', controlQuestion);
 
    // Insert the "totals" elements
    var insertedEl = '<li class="multiplenumerichelp help-item">\
              <span class="label">Remaining: </span>\
              <span class="dynamic_remaining">\
                <span>100</span>\
              %</span>\
            </li>\
            <li class="multiplenumerichelp  help-item">\
              <span class="label">Total: </span>\
              <span class="dynamic_sum">\
                <span>0</span>\
              %</span>\
            </li>';
    $('ul.subquestions-list', thisQuestion).append(insertedEl);
 
    // A listener on the inputs
    $(inputs).bind('change keyup', function(e) {
      handleInputs(thisQuestion, controlQuestion);
    });
    $(inputs).bind('paste', function(e) {
      setTimeout(function () {
        handleInputs(thisQuestion, controlQuestion);
      }, 100);
    });
 
    // Initialize sum values
    handleInputs(thisQuestion);
 
  });
 
  // A function to calculate totals in a multiple numeric
  function handleInputs(thisQuestion, controlQuestion) {
    var thisQuestion = thisQuestion;
    var controlQuestion = controlQuestion;
    var controlTotal = 0;
    var answered = 0;
    var sumTotal = 0;
    $('input[type=text]', controlQuestion).each(function(i) {
      controlTotal = Number(controlTotal) + Number($(this).val());
    });
    $('input[type=text]', thisQuestion).each(function(i) {
      sumTotal = Number(sumTotal) + Number($(this).val());
      if($(this).val() != '') {
        answered = Number(answered) + 1;
      }
    });
    $('span.dynamic_remaining span', thisQuestion).text(controlTotal - sumTotal);
    $('span.dynamic_sum span', thisQuestion).text(sumTotal);
    $('span.dynamic_sum', thisQuestion).removeClass('good error');
    if(answered > 0 &amp;&amp; sumTotal == controlTotal) {
      $('span.dynamic_sum', thisQuestion).addClass('good');
    }
    else if(answered > 0) {
      $('span.dynamic_sum', thisQuestion).addClass('error');
    }
  }
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 3 months ago by tpartner.
The topic has been locked.
  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #114809 by slaugh
It doesn't seem to work. May be I completed the code wrong...
It still prevents the participant to go to the next page even if the totals don't correspond.
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the elements
    var PROVSAL= $('#question{13560}');
    var NBEMPLOI= PROVSAL.prevAll('.numeric-multi:eq(0)');
    var inputs = $('input[type=text]', PROVSAL).add('input[type=text]', NBEMPLOI);
 
    // Insert the "totals" elements
    var totalvalue_13558= '<li class="multiplenumerichelp help-item">\
              <span class="label">Restant: </span>\
              <span class="dynamic_remaining">\
                <span>100</span>\
              %</span>\
            </li>\
            <li class="multiplenumerichelp  help-item">\
              <span class="label">Total: </span>\
              <span class="dynamic_sum">\
                <span>0</span>\
              %</span>\
            </li>';
    $('ul.subquestions-list', PROVSAL).append(totalvalue_13560);
 
    // A listener on the inputs
    $(inputs).bind('change keyup', function(e) {
      handleInputs(PROVSAL, NBEMPLOI);
    });
    $(inputs).bind('paste', function(e) {
      setTimeout(function () {
        handleInputs(PROVSAL, NBEMPLOI);
      }, 100);
    });
 
    // Initialize sum values
    handleInputs(PROVSAL);
 
  });
 
  // A function to calculate totals in a multiple numeric
  function handleInputs(PROVSAL, NBEMPLOI) {
    var PROVSAL= PROVSAL;
    var NBEMPLOI = NBEMPLOI;
    var controlTotal = 0;
    var answered = 0;
    var sumTotal = 0;
    $('input[type=text]', NBEMPLOI).each(function(i) {
      controlTotal = Number(controlTotal) + Number($(this).val());
    });
    $('input[type=text]', PROVSAL).each(function(i) {
      sumTotal = Number(sumTotal) + Number($(this).val());
      if($(this).val() != '') {
        answered = Number(answered) + 1;
      }
    });
    $('span.dynamic_remaining span', PROVSAL).text(controlTotal - sumTotal);
    $('span.dynamic_sum span', PROVSAL).text(sumTotal);
    $('span.dynamic_sum', PROVSAL).removeClass('good error');
    if(answered > 0 &amp;&amp; sumTotal == controlTotal) {
      $('span.dynamic_sum', PROVSAL).addClass('good');
    }
    else if(answered > 0) {
      $('span.dynamic_sum', PROVSAL).addClass('error');
    }
</script>D’o&amp;ugrave; proviennent vos salari&amp;eacute;s permanents et saisonniers?
<p>
   </p>
<p>
  <span style="font-size:11px;"><em>Veuillez inscrire le nombre de salari&amp;eacute;s pour chacune des r&amp;eacute;ponses correspondant &amp;agrave; votre situation.</em></span></p>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #114810 by tpartner
Did you remove all of the other restrictions from the questions?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #114811 by slaugh
When I remove all the other restrictions...total doesnt show at all.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #114814 by tpartner
Here is a working sample survey:

File Attachment:

File Name: limesurvey...1-23.lss
File Size:21 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #143133 by slaugh
I am trying to use the code you gave me a year ago for a new survey.
However, it doesn't seem to work.
I am working with LS Version 2.52+160920 and the default theme.

Here a link to the survey:

It is for the 6th question: Quelle part de votre chiffre d’affaires provient de chacun de ces canaux de mise en marché ?
The total should be 100 % but I don't want to prevent participant from filling the survey if the total is not 100%.

Thank you!

Sophie
The topic has been locked.
  • slaugh
  • slaugh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #143134 by slaugh
Oops here is the link: survey.cirano.qc.ca/index.php/171722?lang=fr

And I am talking about that code:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

// Identify this question
var thisQuestion = $('#question{QID}');

// Insert the "totals" elements
var insertedEl = '<li class="multiplenumerichelp help-item">\
<span class="label">Remaining: </span>\
<span class="dynamic_remaining">\
<span>100</span>\
%</span>\
</li>\
<li class="multiplenumerichelp help-item">\
<span class="label">Total: </span>\
<span class="dynamic_sum">\
<span>0</span>\
%</span>\
</li>';
$('ul.subquestions-list', thisQuestion).append(insertedEl);

// A listener on the inputs
$('input[type=text]', thisQuestion).bind('change keyup', function(e) {
handleInputs(thisQuestion);
});
$('input[type=text]', thisQuestion).bind('paste', function(e) {
setTimeout(function () {
handleInputs(thisQuestion);
}, 100);
});

// Initialize sum values
handleInputs(thisQuestion);

});

// A function to calculate totals in a multiple numeric
function handleInputs(question) {
var thisQuestion = $(question);
var answered = 0;
var sumTotal = 0;
$('input[type=text]', thisQuestion).each(function(i) {
sumTotal = Number(sumTotal) + Number($(this).val());
if($(this).val() != '') {
answered = Number(answered) + 1;
}
});
$('span.dynamic_remaining span', thisQuestion).text(100 - sumTotal);
$('span.dynamic_sum span', thisQuestion).text(sumTotal);
$('span.dynamic_sum', thisQuestion).removeClass('good error');
if(answered > 0 && sumTotal == 100) {
$('span.dynamic_sum', thisQuestion).addClass('good');
}
else if(answered > 0) {
$('span.dynamic_sum', thisQuestion).addClass('error');
}
}
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143142 by tpartner
The HTML structure of that question type was changed in LS 2.5x.

Here is an updated script to accommodate those changes (note that the HTML structure may be changed again in future releases).

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Insert the "totals" elements
    var insertedEl = '<tr>\
              <td class="hide-on-small-screen"></td>\
              <td>\
                <div class="multiplenumerichelp help-block pull-right">\
                  <div class="label label-default">\
                    <label>Restant :</label>\
                    <span class="dynamic_remaining"><span>100</span>%</span>\
                  </div>\
                </div>\
              </td>\
            </tr>\
            <tr>\
              <td class="hide-on-small-screen"></td>\
              <td>\
                <div class="multiplenumerichelp help-block pull-right">\
                  <div class="label label-default">\
                    <label class="">Total :</label>\
                    <span class="dynamic_sum"><span>0</span>%</span>\
                  </div>\
                </div>\
              </td>\
            </tr>';
    $('tr.answer-item:last', thisQuestion).after(insertedEl);
 
    // A listener on the inputs
    $('input[type=text]', thisQuestion).bind('change keyup', function(e) {
      handleInputs(thisQuestion);
    });
    $('input[type=text]', thisQuestion).bind('paste', function(e) {
      setTimeout(function () {
        handleInputs(thisQuestion);
      }, 100);
    });
 
    // Initialize sum values
    handleInputs(thisQuestion);
  });
 
  // A function to calculate totals in a multiple numeric
  function handleInputs(question) {
    var thisQuestion = $(question);
    var answered = 0;
    var sumTotal = 0;
    $('input[type=text]', thisQuestion).each(function(i) {
      sumTotal = Number(sumTotal) + Number($(this).val());
      if($(this).val() != '') {
        answered = Number(answered) + 1;
      }
    });
    $('span.dynamic_remaining span', thisQuestion).text(100 - sumTotal);
    $('span.dynamic_sum span', thisQuestion).text(sumTotal);
    $('span.dynamic_sum', thisQuestion).removeClass('good error');
    if(answered > 0 &amp;&amp; sumTotal == 100) {
      $('span.dynamic_sum', thisQuestion).addClass('good');
    }
    else if(answered > 0) {
      $('span.dynamic_sum', thisQuestion).addClass('error');
    }
  }
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...-2-3.lss
File Size:21 KB

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