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

More
11 years 9 months ago #80879 by Qat
Hello everyone

Sorry to bother you with this question, but it would take me hours to gain enough knowledge in javascript/limesurvey syntax to perform this simple task :

Context : I have a Non Mandatory Table Question and the option Hide "No answer" is active at survey level. So every line of the table initially diplays an empty row of radio buttons, but when one choice has been made for one row, the user can't revert to "no answer" (since it is hidden).

Better with an exemple : The question is "What's your opinion about the food". The rows are "Temperature, Taste, Quantity and Suited to children". The options are "Good, Neutral, Bad and (hidden) No answer"

We want the user to feel that he must answer the first rows, so we don't want to display the "no answer" option. But people without children should be able to revert to an empty row, if they mistakenly checked something.

My idea is to insert a button inside the subquestion, and use javascript to revert the radiobutton to an empty state.

FYI, I use v1.91, the question ID is 139, the Subquestion is 3x3a5, so the code I get for one on of the radiobuttons on that row is :

<input class="radio" type="radio" name="81328X6X1393x3a5" value="C04h2" id="answer81328X6X1393x3a5-C04h2" title="Neutral " onclick="noop_checkconditions(this.value, this.name, this.type)">

Can you help me with this one ?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 9 months ago #80891 by tpartner
1) Set up your survey to use JavaScript .

2) Add the following script to the source of the array. Replace "QQ" with the array question ID .

The script inserts a button in each answer-text cell of the array. When clicked, the button unchecks all radios in that row.
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function(){
 
    addReset(QQ);
 
    function addReset(qID) {
      $('#question'+qID+' th.answertext').append('<input type="button" value="Reset" class="resetButton" />');
 
      $('#question'+qID+' .resetButton').click(function(e){
        var el = $(this).parents('tr:eq(0)');
        $('input.radio', el).attr('checked', false);
      });
    }
  });
 
</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: kachne
The topic has been locked.
More
11 years 9 months ago - 11 years 9 months ago #80910 by Qat
Thanks a lot for your reply. It works just as you say.

Now if i may, I have one more question to fit perfectly my initial request :

Your solution inserts a reset button for each 'th'(row header) inside the question with the id I provide.

I would like to insert the reset button in only one specific row header.
When I inspect the code of the page, I can see that each row is enclosed in a tbody with a specidic ID. So I tried to replace the question ID by the specific tbody ID, but this didn't work :
Code:
<script type="text/javascript" charset="utf-8">   
$(document).ready(function(){     
addReset(javatbd81328X6X1393x3a5);     
function addReset(tbID) {
  $('#tbody'+tbID+' th.answertext').append('<input type="button" value="Reset" class="resetButton" />'); 
  $('#tbody'+tbID+' .resetButton').click(function(e){
    var el = $(this).parents('tr:eq(0)');
    $('input.radio', el).attr('checked', false);
 });
 }
 });
</script>
 
Is it possible to achieve this ? What am I doing wrong ?
Thanks for your replies !
Last edit: 11 years 9 months ago by Qat.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 9 months ago #80924 by tpartner
You were close...
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function(){
 
    addReset('javatbd81328X6X1393x3a5');
 
    function addReset(tbID) {
      $('#'+tbID+' th.answertext').append('<input type="button" value="Reset" class="resetButton" />');
 
      $('#'+tbID+' .resetButton').click(function(e){
        var el = $(this).parents('tr:eq(0)');
        $('input.radio', el).attr('checked', false);
      });
    }
  });
 
</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.
  • Jules
  • Jules's Avatar
  • Visitor
  • Visitor
10 years 7 months ago #98387 by Jules
Hi everybody,

I am wondering if there is a possibility to change the positioning of the reset button. Would it be possible to…

a) …place it on the right side behind the cells with the answers (i.e.,: Question text | Option 1| Option 2| Option 3| Reset button)?

or

b) …have a reset button for the whole question (i.e., if there a five subquestions there would be one reset button which resets all choice for all five subquestions)?

Thanks in advance for any comments and suggestions!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 7 months ago #98391 by tpartner
What version of LimeSurvey are you using?

Where would you want the whole-question reset button to be placed?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Jules
  • Jules's Avatar
  • Visitor
  • Visitor
10 years 7 months ago #98393 by Jules
Hi,

I am using Version 2.00+ (Build 130611) and I think the button would best fit below the subquestions, for instance in the lower right corner of the question.

Thanks a lot!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 7 months ago #98398 by tpartner
To insert a row reset button for every row at the right, add this to the source of the question text:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function(){
 
    addReset('{QID}');
 
    function addReset(qID) {
      $('#question'+qID+' table.subquestions-list thead tr').append('<th />');
      $('#question'+qID+' tr.answers-list').append('<td class="buttonCell"><input type="button" value="Reset" class="resetButton" /></td>');
 
      $('#question'+qID+' .resetButton').click(function(e){
        var parentRow = $(this).closest('tr');
        $('input.radio', parentRow).attr('checked', false);
      });
    }
  });
</script>


To insert a question rest button below and to the right of the array, add this to the source of the question text:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function(){
 
    addReset('{QID}');
 
    function addQuestionReset(qID) {
      $('#question'+qID+' table.subquestions-list').after('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');
 
      $('#question'+qID+' .resetQuestionButton').click(function(e){
        var parentQuestion = $(this).closest('.array-flexible-row');
        $('input.radio', parentQuestion).attr('checked', false);
      });
    }
  });
</script>

Attachment Capture_2013-07-23.JPG not found


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: Jules
The topic has been locked.
  • Jules
  • Jules's Avatar
  • Visitor
  • Visitor
10 years 7 months ago #98429 by Jules
Hi Tony,

Thanks a lot for your help.

Unfortunately, the second version (add reset button for the whole question) does not work with me. I don't know what's wrong... I even tried to replicate it using the exact same layout that you used but I could not get it to work.

Concerning the first option, I have a follow up question. Using my layout, you have to scroll to access the reset button. Is there any way to avoid that?

Attachment Reset.JPG not found



I tried to add this to the template.css hoping that the remaining 15% would be taken for the reset button but the code does not affect the layout of my question at all.

#question18463 col.col-answers {
width: 25% !important;
}
#question18463 col.odd,
#question18463 col.even {
width: 20% !important;
}

Thanks again!
Julia
Attachments:
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 7 months ago #98434 by DenisChenu
Julia,

For citronade, table are set to table-layout:fixed.

Than update template to have
table-layout:auto.

Denis

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: Jules
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 7 months ago #98452 by tpartner

Unfortunately, the second version (add reset button for the whole question) does not work with me.

It works fine for me in citronade. Have you added any other JavaScript? Any JavaScript errors?

Using my layout, you have to scroll to access the reset button.

Yeah, Denis is correct, if using citronade, you need too change the table-layout rule:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function(){
 
    addReset('{QID}');
 
    function addReset(qID) {
      $('#question'+qID+' table.subquestions-list').css('table-layout', 'auto');
      $('#question'+qID+' table.subquestions-list thead tr').append('<th />');
      $('#question'+qID+' tr.answers-list').append('<td class="buttonCell"><input type="button" value="Reset" class="resetButton" /></td>');
 
      $('#question'+qID+' .resetButton').click(function(e){
        var parentRow = $(this).closest('tr');
        $('input.radio', parentRow).attr('checked', false);
      });
    }
  });
</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: Jules
The topic has been locked.
  • lsorg
  • lsorg's Avatar
  • Visitor
  • Visitor
10 years 7 months ago - 10 years 7 months ago #98600 by lsorg
Hello,

tpartner

To insert a question rest button below and to the right of the array, add this to the source of the question text:

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

addReset('{QID}');

function addQuestionReset(qID) {
$('#question'+qID+' table.subquestions-list').after('<input type="button" style="float:right;" value="Reset Question" class="resetQuestionButton" />');

$('#question'+qID+' .resetQuestionButton').click(function(e){
var parentQuestion = $(this).closest('.array-flexible-row');
$('input.radio', parentQuestion).attr('checked', false);
});
}
});
</script>

The code works very well. But not for array dual scale. Knows everyone how I have to change the code that the reset button works with the array dual scale?

Best regards
lsorg
Last edit: 10 years 7 months ago by lsorg.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose