Check out the LimeSurvey source code on GitHub!

Preselect dropdown list?

5 years 4 months ago - 5 years 4 months ago #66546 by waitz
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 |

Please Log in to join the conversation.

5 years 4 months ago #66548 by Mazi

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)survey-consulting.com'"

Please Log in to join the conversation.

5 years 4 months ago #66549 by waitz
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 |

Please Log in to join the conversation.

5 years 4 months ago #66554 by tpartner
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.

Please Log in to join the conversation.

5 years 4 months ago #66555 by waitz
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 |

Please Log in to join the conversation.

5 years 4 months ago #66560 by waitz
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 |

Please Log in to join the conversation.

5 years 4 months ago #66562 by tpartner
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.

Please Log in to join the conversation.

5 years 4 months ago #66565 by waitz
Thank you, thank you, thank you :cheer:

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

5 years 4 months ago #66577 by Mazi
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)survey-consulting.com'"

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form