Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: Prefill for Array(Numbers)

Prefill for Array(Numbers) 3 years 2 months ago #100109

  • OlgaN
  • OlgaN's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 1
  • Karma: 0
Hi,

I have two columns and several rows in a question of type Array(Number), each answer drop-down list contains 60 options, which are numbers from 1 to 60. I would like to pre-fill all the drop-down lists so when the page is loaded they already have zeros as chosen options.

I tried two ways but non of them is working:

document.getElementById("#answer{SID}X{GID}X{self.qid}SQ001_SQ001").value = "0";

and

var selectId = 'answer{SID}X{GID}X{self.qid}SQ001_SQ001';
var defaultVal = 'input[id='+selectId+']';
$(defaultVal).val(0);

Could you, please, help me to find out what I am doing wrong and how it should be.
Thanks.
The administrator has disabled public write access.

Prefill for Array(Numbers) 3 years 2 months ago #100199

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6571
  • Thank you received: 1477
  • Karma: 579
Try this:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		var thisQuestion = $('#question{QID}');
 
		$('select', thisQuestion).each(function(i) {
 
			if($(this).val() == '') {
				$(this).val(0);
			}
		});
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: OlgaN

Prefill for Array(Numbers) 6 months 3 weeks ago #136081

  • Anana
  • Anana's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Hi,

I'm trying to do the same thing (pre-filling array (numbers)), but based on some token attributes. For instance, if my token attributes 3 =1, the number of the array should be 1. I know how to do this fow single answer questions, dropdown or multiple answers choice, but it doesn't work for this array (numbers), as numbers are not answers options I defined (but automatically generated according to the range I chose). Would you have a solution for this?

Thanks!
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136091

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6571
  • Thank you received: 1477
  • Karma: 579
LimeSurvey version?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136109

  • Anana
  • Anana's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Version 1.92+ Build 120822
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136126

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6571
  • Thank you received: 1477
  • Karma: 579
Assuming your array uses the default dropdown layout, try this:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		var thisQuestion = $('#question{QID}');
 
		$('select', thisQuestion).each(function(i) {
 
			if($(this).val() == '' && {TOKEN:ATTRIBUTE_3} == 1) {
				$(this).val(1);
			}
		});
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136127

  • Anana
  • Anana's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Thanks!! Indeed it works! However, I used the techniques you described in this topic: www.limesurvey.org/forum/can-i-do-this-w...my-solution&start=20
In order to have two columns with related question side by side.
Then, when using the code you just provided above, it fills all the possible answers (see caption attached: for "goals", I want to let the choice to add from 1 to 6 goals, all being different). Then, ideally, when prefilling it would only fill one row.
Attachments:
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136144

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6571
  • Thank you received: 1477
  • Karma: 579
This script will only affect one row. Adjust the value for "rowNumber" as required.

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		var rowNumber = 3;
 
		var thisQuestion = $('#question{QID}');
 
		$('tbody[id^="javatbd"]:eq('+(rowNumber-1)+') select', thisQuestion).each(function(i) {
 
			if($(this).val() == '' && {TOKEN:ATTRIBUTE_1} == 3) {
				$(this).val(1);
			}
		});
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136147

  • Anana
  • Anana's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Thank you Tony, super useful! It works perfectly!

Cheers,
The administrator has disabled public write access.

Prefill for Array(Numbers) 6 months 3 weeks ago #136152

  • Anana
  • Anana's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 8
  • Karma: 0
Hi Tony,

There is still a small issue as this code then prefill all the columns from row 1. My second column is an array flexi, with a dropdown option and several answers options (different from the one in the first column). I'm trying to add a prefilled answer for the second column then, based on your code, but it does not work. Do you understand what I missed? I tried to add this at the end of the previous code:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var rowNumber = 1;
var nextArrayFlexi = $(thisQuestion).nextAll('.array-flexible-row:eq(0)');
$('tbody[id^="javatbd"]:eq('+(rowNumber-1)+') select', nextArrayFlexi).each(function(i) {

if($('#answer38934X2077X29246').val() == '') && {TOKEN:ATTRIBUTE_9} == ‘1.2’) {
$('#answer38934X2077X29246 option').removeAttr('selected');
$('#answer38934X2077X29246 option[value="A13"]').attr('selected', 'selected');
$('#answer38934X2077X29246').val('A13');
}
});
</script>

Thank you so much!
The administrator has disabled public write access.
Time to create page: 0.438 seconds
Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form