Check out the LimeSurvey source code on GitHub!

Assigning default values to numerical array in subquestions

More
2 years 7 months ago #111067 by wstahl09
Hello,

I have a mandatory question that prompts the user to indicate the days and hours since an event, which is saved into a numerical array. I was wondering if there was anyway to set a default value of 0 to both days and hours, and if not how I could go about doing so with javascript.

Thanks!

Please Log in to join the conversation.

More
2 years 7 months ago - 2 years 7 months ago #111074 by tpartner
Are you using an Array (Numbers)(Checkbox) or Array (Texts)(Numbers only) question type?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 2 years 7 months ago by tpartner.

Please Log in to join the conversation.

More
2 years 3 months ago #113976 by MVT
Hi,

I tried a tip/workaround find here but...
I've an array of numbers with the checkbox option.
I've added this code (found in the workaround page) :
<script type="text/javascript" charset="utf-8">
   $(document).ready(function() {
       // A function to pre-check a column of an array
       function checkedDefault(qID, column) {
           var checkedCol = column - 1;
           $('#question' + qID + ' table.question tbody tr').each(function(i) {
               if ($('input.checkbox[checked=true]', this).length == 0) {
                   $('input.checkbox:eq(' + checkedCol + ')', this).attr('checked', true);
               }
           });
       }
       // Call the function with a question ID and column number
       checkedDefault(165, 1);
   });
</script>

It works well but...
My question is mandatory. The values (boexe checked) ​​do not appear to be taken into account.

Do I miss something ?
For the moment, I put my question "optional..."

Is it possible (for check boxes) to check on column for all the values in that column ?

Thanks

Please Log in to join the conversation.

More
2 years 3 months ago #113993 by tpartner
Function updated for 2.05...

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
		// A function to pre-check a column of an array
		function checkedDefault(qID, column) {
			var checkedCol = column - 1;
			$('#question'+qID+' tr.subquestions-list').each(function(i) {
				if ($('input.checkbox:checked', this).length == 0) {
					$('input.checkbox:eq('+checkedCol+')', this).prop('checked', true);
					$('input.checkbox:eq('+checkedCol+')', this).parent().find('input[type="hidden"]').val(1);
				}
			});
		}
		// Call the function with a question ID and column number
		checkedDefault(165, 1);
	});
 
</script>

And I've updated the workaround page - manual.limesurvey.org/Workarounds:_Manip...s_in_array_questions


.

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.

More
2 years 3 months ago #113998 by MVT
Thanks tpartner.

I'll test it today.
Is it possible in the same function (I suppose a part of code between <script> </script>) to uncheck the first column (NSP in French, NA or NO ? in English ?) if one or more options are checked on the same line?

If a user save his survey to complete it at another moment, are his answers saved and not "resetted" by this script?.
I suppose we can do something like a sum of all the columns (except the column passed by the parameter) in a line and check only if the sum = 0.

I'll try this if I find a few hours. I'm a very "newbee" in JS !

In the snippet, I've replaced input.radio bu input.checkbox (in the original release)

Regards

Please Log in to join the conversation.

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