- Posts: 2
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
How to combine two functions in one question?
- Till
- Topic Author
- Offline
- New Member
Less
More
7 years 9 months ago - 7 years 9 months ago #137057
by Till
How to combine two functions in one question? was created by Till
We design a survey for a Social Network Analysis in which we ask the employees in a company to nominate colleagues for certain relationship types. For instance, with whom are you friends outside of work?
An array lists all employees and participants can make their choices.
Function 1: We use a radio buttons for nominating colleagues. To undo a nomination we’ve implemented a reset button next to each name (see screenshot). That works
Function 2: Now we would like to insert subheadings in order to group the individual names (by organisational departments) to make it easier for participants to find their peers (there will be 70 names in total). We’ve tried the “hide” function to omit an answer and to display the department name instead. That works without the reset button, but not in combination. We also tried the function “subheading”, but also that did not work.
I'm rather new to LimeSurvey and coding, and I'm not entirely sure how to combine functions properly.
How could we combine the reset button with either the “hide” function or the ”subheading” function?
Or, is there a better way to group answers?
Thanks,
Till
We use LimeSurvey Version 2.05+. This is how the script for the radio button looks like (in the question source):
<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>
An array lists all employees and participants can make their choices.
Function 1: We use a radio buttons for nominating colleagues. To undo a nomination we’ve implemented a reset button next to each name (see screenshot). That works
Function 2: Now we would like to insert subheadings in order to group the individual names (by organisational departments) to make it easier for participants to find their peers (there will be 70 names in total). We’ve tried the “hide” function to omit an answer and to display the department name instead. That works without the reset button, but not in combination. We also tried the function “subheading”, but also that did not work.
I'm rather new to LimeSurvey and coding, and I'm not entirely sure how to combine functions properly.
How could we combine the reset button with either the “hide” function or the ”subheading” function?
Or, is there a better way to group answers?
Thanks,
Till
We use LimeSurvey Version 2.05+. This is how the script for the radio button looks like (in the question source):
<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>
Attachments:
Last edit: 7 years 9 months ago by Till.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
7 years 9 months ago #137086
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic How to combine two functions in one question?
Do you simply want to add the "subheadings" as new rows in the array?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Till
- Topic Author
- Offline
- New Member
Less
More
- Posts: 2
- Thank you received: 0
7 years 9 months ago #137102
by Till
Replied by Till on topic How to combine two functions in one question?
Hi Tony,
yes, that's right. I would like to add subheadings into the array.
The result should look like this:
Subheading 1
Name 1
Name 2
...
Subheading 2
Name 3
Name 4
...
In my case, a subheading is the name of a department and the names are staff members who work there.
The important bit is to keep a Reset Button for each name.
Thanks,
Till
yes, that's right. I would like to add subheadings into the array.
The result should look like this:
Subheading 1
Name 1
Name 2
...
Subheading 2
Name 3
Name 4
...
In my case, a subheading is the name of a department and the names are staff members who work there.
The important bit is to keep a Reset Button for each name.
Thanks,
Till
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
7 years 9 months ago #137105
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic How to combine two functions in one question?
I believe something like this should let you insert "header" rows without affecting your reset buttons:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Define the sub-heading text strings var subHeading1 = 'Subheading 1'; var subHeading2 = 'Subheading 2'; var answersLength = $('tr.answers-list:eq(0) td.answer-item', thisQuestion).length; // Insert the new rows $('tr.answers-list:eq(0)', thisQuestion).before('<tr class="sub-header-row"><th>'+subHeading1+'</th><td colspan="'+answersLength+'" /></tr>'); $('tr.answers-list:eq(5)', thisQuestion).before('<tr class="sub-header-row"><th>'+subHeading2+'</th><td colspan="'+answersLength+'" /></tr>'); // Fix up the row classes var rowClass = 1; $('table.subquestions-list tbody tr', thisQuestion).each(function(i) { if($(this).hasClass('sub-header-row')) { rowClass = 1 } else { rowClass++; $(this).removeClass('array1 array2') if(rowClass % 2 == 0) { $(this).addClass('array2'); } else { $(this).addClass('array1'); } } }); }); </script>
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: nique, bdeprez
The topic has been locked.
- SabrinaD
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
6 years 9 months ago #155219
by SabrinaD
Replied by SabrinaD on topic How to combine two functions in one question?
Hi,
I also try to add subheadings in my array question which works fine with the code provided here by tpartner.
Atually I added them to a Semantic Differential Array and it worked just fine.
However I would need my subheadings to be centered above each line - How can I do that?
I'm relatively new to limesurvey and have little experience with adding javascript.
I attach a picture of what the array should look like.
Thanks in advance
Sabrina
I also try to add subheadings in my array question which works fine with the code provided here by tpartner.
Atually I added them to a Semantic Differential Array and it worked just fine.
However I would need my subheadings to be centered above each line - How can I do that?
I'm relatively new to limesurvey and have little experience with adding javascript.
I attach a picture of what the array should look like.
Thanks in advance
Sabrina
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
6 years 9 months ago #155220
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic How to combine two functions in one question?
This script will center the sub-headings:
Then, maybe something like this at the end of template.css:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Define the sub-heading text strings var subHeading1 = 'Subheading 1'; var subHeading2 = 'Subheading 2'; var columnsLength = $('tr.answers-list:eq(0) > *', thisQuestion).length; // Insert the new rows $('tr.answers-list:eq(0)', thisQuestion).before('<tr class="sub-header-row"><th colspan="'+columnsLength+'">'+subHeading1+'</th></tr>'); $('tr.answers-list:eq(2)', thisQuestion).before('<tr class="sub-header-row"><th colspan="'+columnsLength+'">'+subHeading2+'</th></tr>'); // Fix up the row classes var rowClass = 1; $('table.subquestions-list tbody tr', thisQuestion).each(function(i) { if($(this).hasClass('sub-header-row')) { rowClass = 1 } else { rowClass++; $(this).removeClass('array1 array2') if(rowClass % 2 == 0) { $(this).addClass('array2'); } else { $(this).addClass('array1'); } } }); }); </script>
Then, maybe something like this at the end of template.css:
Code:
.sub-header-row th { background-color: #547599; color: #FFFFFF; text-align: center; }
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- SabrinaD
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
6 years 9 months ago #155221
by SabrinaD
Replied by SabrinaD on topic How to combine two functions in one question?
Thanks for your quick reply. I copy and pasted your code, but unfortunately my subheadings are still aligned on the left. (see screen shot)
Could it be a problem that I use none of the standard templates? I made a copy of the default template and changed some background colours.
Cheers,
Sabrina
Could it be a problem that I use none of the standard templates? I made a copy of the default template and changed some background colours.
Cheers,
Sabrina
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
6 years 9 months ago #155222
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic How to combine two functions in one question?
Can you give a link to a live test survey containing only that question?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- SabrinaD
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
6 years 9 months ago #155223
by SabrinaD
Replied by SabrinaD on topic How to combine two functions in one question?
here is the screenshot
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
6 years 9 months ago #155224
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic How to combine two functions in one question?
A screenshot doesn't help. I will need to see it live to debug it.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- SabrinaD
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
6 years 9 months ago #155225
by SabrinaD
Replied by SabrinaD on topic How to combine two functions in one question?
I'll post the link in 2 minutes
The topic has been locked.
- SabrinaD
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
6 years 9 months ago #155226
by SabrinaD
Replied by SabrinaD on topic How to combine two functions in one question?
websurvey.slu.se/index.php?r=survey/index&sid=251466&lang=sv
here comes my link
Tanks so much for having a look!
Cheers
Sabrina
here comes my link
Tanks so much for having a look!
Cheers
Sabrina
The topic has been locked.