Welcome, Guest
Username: Password: Remember me

TOPIC: Default Date

Default Date 1 year 8 months ago #84744

  • LSCitizen
  • LSCitizen's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 33
  • Karma: 0
Hello -

I am using the date question type with dropdown boxes.
Is it possible to set the question to the current date as a default.
I would like users to still be able to change the date, if needed.
Thanks

LimeSurvey Version 1.92+, Build 120718
The administrator has disabled public write access.

Re: Default Date 1 year 8 months ago #84813

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3837
  • Thank you received: 688
  • Karma: 330
1) Set up your survey to use JavaScript.

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

If no previously entered date is detected, the dropdowns are defaulted to the current date.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		todaysDate(QQ);
 
		function todaysDate(qID, col1Max, col2Max) {
 
			function addZero(num) {
				(String(num).length < 2) ? num = String("0" + num) :  num = String(num);
				return num;		
			}
 
			var currentDate = new Date();											 
			var currentYear = currentDate.getFullYear();											 
			var currentMonth = addZero(currentDate.getMonth()+1);											 
			var currentDay = addZero(currentDate.getDate());
 
			// Check for previously entered date, and if none, populate them
			if($('#question'+qID+' input.text').val() == '') {
				$('#question'+qID+' select.day option[value="'+currentDay+'"]').attr('selected', 'selected');
				$('#question'+qID+' select.month option[value="'+currentMonth+'"]').attr('selected', 'selected');
				$('#question'+qID+' select.year option[value="'+currentYear+'"]').attr('selected', 'selected');
			}
		}
 
	});
 
</script>
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.
Last Edit: 1 year 8 months ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: DenisChenu
Moderators: ITEd
Time to create page: 0.097 seconds
Donation Image