Change question labels based the current date

More
4 years 5 months ago #95843 by nuin
nuin created the topic: Change question labels based the current date
Hi

We have a question (array numbers) with X and Y labels, where the Y labels are the past 6 months. Currently we are doing it manually, and my question is if there's a way to automate the process of changing the labels based on the current month of the year.

Thanks in advance

Please Log in to join the conversation.

More
4 years 5 months ago #95861 by tpartner
tpartner replied the topic: Change question labels based the current date
Do you want the months displayed ascending or descending?

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 years 5 months ago #95862 by tpartner
tpartner replied the topic: Change question labels based the current date
Ignore that last question, we can do either with the same function.

Set up your survey to use JavaScript and add the following script to the source of the array question:
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		var qID = {QID};
 
		// Change this to true for ascending months
		var ascending = false;		
 
		// Find the current month
		var todayDate = new Date();
		var todayMonth = todayDate.getMonth();
 
		// Month labels
		var months = new Array();
		months[0]="January";
		months[1]="February";
		months[2]="March";
		months[3]="April";
		months[4]="May";
		months[5]="June";
		months[6]="July";
		months[7]="August";
		months[8]="September";
		months[9]="October";
		months[10]="November";
		months[11]="December";
 
		// Loop through the row labels
		$('#question'+qID+' .answertext').each(function(i) {
 
			// Do we want ascending? (don't edit here)
			if(ascending == true) {
				i = $('#question'+qID+' .answertext').length - (i+1);
			}
 
			// Find the month for this row
			if(todayMonth - i >= 0) {
				var monthIndex = todayMonth - i;
			}
			else {
				var monthIndex = 12 + todayMonth - i;
			}
 
			// Load the row label text
			$(this).text(months[monthIndex]);
		}); 
	});
</script>


The script will insert the months in descending order by default. If you want ascending, change:
var ascending = false;
To:
var ascending = true;

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

Please Log in to join the conversation.

More
4 years 5 months ago #95994 by nuin
nuin replied the topic: Change question labels based the current date
Thanks, works like a charm.

Just for reference, we wanted the months on the column headers, and the solution made the row names to be changed, changing

'#question'+qID+' .answertext'

to

'#question'+qID+' .col-responses'

gave us what we needed.

Thanks a lot

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now