Welcome, Guest
Username: Password: Remember me

TOPIC: Change question labels based the current date

Change question labels based the current date 11 months 1 week ago #95843

  • nuin
  • nuin's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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
The administrator has disabled public write access.

Change question labels based the current date 11 months 1 week ago #95861

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Change question labels based the current date 11 months 1 week ago #95862

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: nuin

Change question labels based the current date 11 months 4 days ago #95994

  • nuin
  • nuin's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.118 seconds
Donation Image