How to combine two functions in one question?

More
1 year 4 months ago - 1 year 4 months ago #137057 by Till
Till created the topic: How to combine two functions in one question?
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>
Attachments:
Last Edit: 1 year 4 months ago by Till.

Please Log in to join the conversation.

More
1 year 4 months ago #137086 by tpartner
tpartner replied the 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.

Please Log in to join the conversation.

More
1 year 4 months ago #137102 by Till
Till replied the 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

Please Log in to join the conversation.

More
1 year 4 months ago #137105 by tpartner
tpartner replied the 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:

<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

Please Log in to join the conversation.

More
4 months 3 weeks ago #155219 by SabrinaD
SabrinaD replied the 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
Attachments:

Please Log in to join the conversation.

More
4 months 3 weeks ago #155220 by tpartner
tpartner replied the topic: How to combine two functions in one question?
This script will center the sub-headings:

<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:

.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:

Please Log in to join the conversation.

More
4 months 3 weeks ago #155221 by SabrinaD
SabrinaD replied the 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

Please Log in to join the conversation.

More
4 months 3 weeks ago #155222 by tpartner
tpartner replied the 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.

Please Log in to join the conversation.

More
4 months 3 weeks ago #155223 by SabrinaD
SabrinaD replied the topic: How to combine two functions in one question?
here is the screenshot
Attachments:

Please Log in to join the conversation.

More
4 months 3 weeks ago #155224 by tpartner
tpartner replied the 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.

Please Log in to join the conversation.

More
4 months 3 weeks ago #155225 by SabrinaD
SabrinaD replied the topic: How to combine two functions in one question?
I'll post the link in 2 minutes

Please Log in to join the conversation.

More
4 months 3 weeks ago #155226 by SabrinaD
SabrinaD replied the 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

Please Log in to join the conversation.

More
4 months 3 weeks ago #155227 by tpartner
tpartner replied the topic: How to combine two functions in one question?
It's a caching issue in your browser, completely clear your cache before testing.

Here's what I see:


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: LouisGac

Please Log in to join the conversation.

More
4 months 3 weeks ago #155228 by SabrinaD
SabrinaD replied the topic: How to combine two functions in one question?
It works! Now i feel slightly embarrassed :)

Thank you very much for your help - you saved my survey (and my PhD ;) )

Cheers,
Sabrina

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now