Array(Text) default answers in LS 3.X

Más
4 meses 2 semanas antes #186694 por 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 .

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

LimeSurvey Partners
Más
4 meses 2 semanas antes #186729 por tpartner
Respuesta de tpartner sobre el tema Array(Text) default answers in LS 3.X
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

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Más
4 meses 2 semanas antes - 4 meses 2 semanas antes #186743 por 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.
Adjuntos:
Última Edición: 4 meses 2 semanas antes por frederikbrandenstein.

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Más
4 meses 2 semanas antes #186781 por tpartner
Respuesta de tpartner sobre el tema Array(Text) default answers in LS 3.X
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

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Más
4 meses 2 semanas antes #186783 por 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 .

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Más
4 meses 2 semanas antes #186786 por tpartner
Respuesta de tpartner sobre el tema Array(Text) default answers in LS 3.X
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

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Más
4 meses 2 semanas antes #186848 por frederikbrandenstein
Thank you very much. It works not only in the MWE above but also in my productive survey.

Regards.

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

¡Comenzar ahora!

Simplemente cree una cuenta y comience a usar LimeSurvey hoy mismo.

Registrarse ahora
¡Únase a nuestro boletín informativo!

Iniciar sesión