Array(Text) default answers in LS 3.X

More
4 months 2 weeks ago #186694 by frederikbrandenstein
I use the code as outlined in this answer for masking values with text in dropdown menus.

I would additionally like the answers to default to some string "2".

For LS 2.X there are some solutions given in the forum, like here .

I struggle to adapt it to my LS 3.16installation.

I guess there is sth. that should do what I mean in the section ///Default to "2" in the following code. While it's not working, you most probably get the line of thought:
$(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
     /// Default to "2"  ----- doesn't work :P 
    $('.inserted-select', thisQuestion).each(function(i){
	$(this).closest('.answer-item').prop('value', '2')
    });
 
 
		// Insert selects
		$('.answer-item', thisQuestion).append('<select class="inserted-select form-control list-question-select">\
													<option value="1">+</option>\
													<option value="2">0</option>\
													<option value="3">-</option>\
				<option value="">Keine Angabe</option>\
												</select>');  
 
		// Listeners
		$('.inserted-select', thisQuestion).on('change', function(i) {
			if($(this).val() != '') {
				$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
			}
			else {
				$(this).closest('.answer-item').find('input:text').val('').trigger('change');
			}
		});
 
		// Returning to page
		$('input:text', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('.answer-item');
			var inputText = $.trim($(this).val());
			var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
			$('select.inserted-select', thisCell).val(selectval);
		});
 
		// Clean-up styles
		$('select.inserted-select', thisQuestion).css({
			'max-width': '100%'
		});
		$('input:text', thisQuestion).css({
			'position': 'absolute',
			'left': '-9999em'
		});
	});
 
 

As always, I'd appreciate any help .

Please Log in or Create an account to join the conversation.

LimeSurvey Partners
More
4 months 2 weeks ago #186729 by tpartner
Can you attach a small sample survey (.lss file) containing only the relevant question and your JavaScript?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago - 4 months 2 weeks ago #186743 by frederikbrandenstein
Find an example attached. This works; nagging me is that it doesn't default to some value, let's say "cannot decide" in the example case.
Attachments:
Last edit: 4 months 2 weeks ago by frederikbrandenstein.

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #186781 by tpartner
I think this may do the trick:

<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		var thisQuestion = $('#question{QID}');
 
		// Insert selects
		$('.answer-item', thisQuestion).append('<select class="inserted-select form-control list-question-select">\
													<option value="1">+</option>\
													<option value="2">cannot decide</option>\
													<option value="3">-</option>\
													<option value="">None</option>\
												</select>');  
 
		// Listeners
		$('.inserted-select', thisQuestion).on('change', function(i) {
			if($(this).val() != '') {
				$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
			}
			else {
				$(this).closest('.answer-item').find('input:text').val('').trigger('change');
			}
		});
 
		// Initial state
		$('input:text', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('.answer-item');
			var inputText = $.trim($(this).val());
			var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
			$('select.inserted-select', thisCell).val(selectval);
 
			// Default answer
			var defaultOption = '2';
			if(inputText == '') {
				$(this).val($('select.inserted-select option[value="'+defaultOption+'"]', thisCell).text());
				$('select.inserted-select', thisCell).val(defaultOption);
			}
		});
 
		// Clean-up styles
		$('select.inserted-select', thisQuestion).css({
			'max-width': '100%'
		});
		$('input:text', thisQuestion).css({
			'position': 'absolute',
			'left': '-9999em'
		});
	});
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #186783 by DenisChenu
@tpartner : if i don't mlake error : user can not answer «none» here : moving previous : none become 2.

No ?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #186786 by tpartner
I don't think so. In this case, the (hidden) text inputs are populated with the dropdown option text, not its value.

So, selecting "None" loads the input with "None" so it is not empty on returning to the page.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #186848 by frederikbrandenstein
Thank you very much. It works not only in the MWE above but also in my productive survey.

Regards.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!