Welcome, Guest
Username: Password: Remember me

TOPIC: Preselect dropdown list?

Preselect dropdown list? 3 years 2 weeks ago #66546

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
I am seriously reducing the complexity of my survey by being able to prefill short text fields with this:
<script>
$(document).ready(function() {
$('#answer26532X40X572NAM01').val('{TOKEN:FIRSTNAME}');
$('#answer26532X40X572NAM02').val('{TOKEN:ATTRIBUTE_1}');
$('#answer26532X40X572NAM03').val('{TOKEN:LASTNAME}');});
</script>
And the next is:
Is it possible to preselect a dropdown list?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 3 years 2 weeks ago by waitz.
The administrator has disabled public write access.

Re: Preselect dropdown list? 3 years 2 weeks ago #66548

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5331
  • Thank you received: 296
  • Karma: 249
waitz wrote:
I am seriously reducing the complexity of my survey by being able to prefill short text fields with this:
<script>
$(document).ready(function() {
$('#answer26532X40X572NAM01').val('{TOKEN:FIRSTNAME}');
$('#answer26532X40X572NAM02').val('{TOKEN:ATTRIBUTE_1}');
$('#answer26532X40X572NAM03').val('{TOKEN:LASTNAME}');});
</script>
And the next is:
Is it possible to preselect a dropdown list?
Have a look at the default values. If this exists for your question type it's the best choice, no need for any additional JS.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Preselect dropdown list? 3 years 2 weeks ago #66549

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Well, I can set a fixed default answer value, but I need one from the tokens (I forgot to specify that)
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: Preselect dropdown list? 3 years 2 weeks ago #66554

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4264
  • Thank you received: 801
  • Karma: 368
Try something like this. Replace "QQ" with the dropdown question ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		var tokenVal = '{TOKEN:ATTRIBUTE_1}';
		var qID = QQ;
 
		$('#question'+qID+' option').filter(function() {
			return $(this).text() == tokenVal;
		}).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.
The administrator has disabled public write access.

[SOLVED] Re: Preselect dropdown list? 3 years 2 weeks ago #66555

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Impressive Tony. I wish I had some of your script knowledge ;)
It is working!
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: [SOLVED] Re: Preselect dropdown list? 3 years 2 weeks ago #66560

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Ahh, one more thing. The country dropdown will be on the same page as the short multiple short text with values from tokens, and to avoid that the submit can reset the whole page if they forget to enter a country, it would be very good to have the same popup alert you made for the multiple short text fields:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		// Interrupt the submit function
		$('form#limesurvey').submit(function () {
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			var empty = 0;
 
			var warningText = 'Please complete the highlighted inputs.';
 
			// Call the mandatory row function with question ID
			mandatoryRow(QQ, 1);
			mandatoryRow(QQ, 3);
 
			// A function to render rows of an array mandatory
			function mandatoryRow(qID, rowNum) {
 
				if ($('div#question'+ qID+ ' input.text:eq('+Number(rowNum - 1)+')').val() == '') {
					$('div#question'+ qID+ ' input.text:eq('+Number(rowNum - 1)+')').css('background-color', 'pink');
					empty = 1;
				} else {
					$('div#question'+ qID+ ' input.text:eq('+Number(rowNum - 1)+')').css('background-color', '#FFFFFF');
				}
			}
 
			if (empty == 1) {
				alert(warningText);
				return false;
			} else {
				return true;
			}
		});
	});
 
</script>
How can I change this code so that it makes sure that a choice from the dropdown is made?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: [SOLVED] Re: Preselect dropdown list? 3 years 2 weeks ago #66562

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4264
  • Thank you received: 801
  • Karma: 368
Add the following script and replace "QQ" with the dropdown question ID:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		dropdownRequired(QQ);
 
		function dropdownRequired(qID) {
 
			var warningText = 'Please select at least one option.';
 
			// Interrupt the submit function
			$('form#limesurvey').submit(function () {
				// Override the built-in "disable navigation buttons" feature
				$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
				if ($('#question'+ qID+ ' option:selected').attr('value') == '') {
					$('#question'+ qID+ ' select').css({'background':'pink'});
					alert(warningText);
					return false;
				}
				else {
					$('#question'+ qID+ ' select').css({'background':''});
					return true;
				}
			});
		}
	});
</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.
The administrator has disabled public write access.

Re: [SOLVED] Re: Preselect dropdown list? 3 years 2 weeks ago #66565

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Thank you, thank you, thank you :cheer:
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: [SOLVED] Re: Preselect dropdown list? 3 years 2 weeks ago #66577

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5331
  • Thank you received: 296
  • Karma: 249
Great, Tony!

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.172 seconds
Donation Image