Welcome to the LimeSurvey Community Forum

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

Reset answers in a table question, using a button and javascript

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 11 months ago #136143 by tpartner
This should do the trick:

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function(){
 
    addQuestionReset('{QID}');
 
    function addQuestionReset(qID) {
      var thisQuestion = $('#question'+qID+'');
 
      $('table.subquestion-list', thisQuestion).after('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');
 
      $('.resetQuestionButton', thisQuestion).click(function(e){
        $('input.radio', thisQuestion).prop('disabled', false);
        $('input.radio:checked', thisQuestion).each(function(i) {
          $(this).prop('checked', false);
          checkconditions('', this.name, this.type);  
        });
      });
    }
  });  
</script>

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
7 years 2 months ago - 7 years 2 months ago #148889 by jsibley
This seems really helpful, but what if I would like to reset the answers in an array to "no answer"? In this version, even that is deselected.

I'm using version 2.5, btw.

Many thanks.
Last edit: 7 years 2 months ago by jsibley.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 2 months ago #148891 by tpartner
This should work with or without "No answer":

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    addQuestionReset('{QID}');
 
    function addQuestionReset(qID) {
      var thisQuestion = $('#question'+qID+'');
 
      $('table.subquestion-list', thisQuestion).after('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');
 
      $('.resetQuestionButton', thisQuestion).click(function(e){
        if($('input.radio[value=""]', thisQuestion).length > 0) {
          $('input.radio[value=""]', thisQuestion).trigger('click');
        }
        else {
          $('input.radio', thisQuestion).prop('disabled', false).prop('checked', false);
        }
        $('input.radio', thisQuestion).each(function(i) {
          checkconditions(this.value, this.name, this.type);  
        });
      });
    }
  });  
</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: jsibley
The topic has been locked.
More
7 years 2 months ago #148892 by jsibley
This is exactly what I was hoping for, thank you.

And, in case it isn't obvious to anyone, you can put the reset button above the array question by changing:

$('table.subquestion-list', thisQuestion).after('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');

to

$('table.subquestion-list', thisQuestion).before('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose