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

TOPIC: How to change default value in array

How to change default value in array 5 years 8 months ago #58252

Actually the subject summarizes it pretty well. How can I change the default value in an array from "No answer" to something else (Limesurvey 1.90)?

Best regards

Martin
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58290

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
If you want to change it for all surveys, see translating LimeSurvey - docs.limesurvey.org/tiki-index.php?page=...anslate%20LimeSurvey.

For individual questions, set up your survey to use JavaScript and add the following to the source of the array. Replace "QQ" with the array ID and modify the text as necessary.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qID = QQ;
		var originalText = 'No answer';
		var newText ='Some new text';
 
		$('#question'+qID+' table.question thead th').filter(function() {
			return $(this).text() == originalText;
		})
		.text(newText);
 
	});
 
</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.

How to change default value in array 5 years 8 months ago #58301

Thank you very much, although I have to admit that I am a little disappointed that a software so mature otherwise requires me to do a JS hack just to change a default value :(
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58302

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Hey guys,

I happen to be trying exactly the same thing. However, although the code runs without any errors indicated by Firebug, nothing happens. Sadly, however, I don't actually know Javascript. I know PHP a bit, but I'm not sure what's happening here, so I basically get stuck pretty quickly trying to debug :-/

This is what I have so far. The question is just an 'array' question type (i.e. radiobuttons). Maybe I misunderstood the original question (because why would anything say "No answer" with this question type?), but as you may understand already, I'm not overly secure about my understanding of Limesurvey or Javascript :-/

This is the code I use now:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qID = '68955X75X1426';
		var originalText = 'No answer';
		var newText ='1';
 
		$('#question'+qID+' table.question thead th').filter(function() {
			return $(this).text() == originalText;
		})
		.text(newText);
 
	});

The code of the first (of 7) answer options I want to be selected by default is 1 (the others are 2-7). The codes of the eight subquestions are also 1-8.

Thank you in advance!

PS: Martin: I have high hopes of Limesurvey 2 - I'm already very impressed by 1.9, but I have a feeling 2 is going to contain some pretty radical improvements. No idea why I have that feeling, but I do :-)
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58304

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
although I have to admit that I am a little disappointed that a software so mature otherwise requires me to do a JS hack just to change a default value
Complaints can be submitted with our paychecks.
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.

How to change default value in array 5 years 8 months ago #58305

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Matherion, there are two problems with your code - you are missing the </script> closing tag and the qID variable is just supposed to be the question ID, not the whole SGQA:



Having said that, I don't think it will achieve your goal.

Do you want the first option in each row selected by default?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 8 months ago by tpartner.
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58309

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Dear tpartner,

sorry, the missing </script> was a copy paste error, my apologies!
And indeed, even corrected it does not achieve my goal :-)

Indeed, I would like to have the first option selected by default. People indicate the degree to which they perform a variety of functions, and most people will only perform one or two functions, so it's a userfriendliness (or, participantfriendliness :-)) thing, not crucial. So if there's no simple answer, please don't bother too much! I should learn how to do this stuff myself at some point anyway :-) In any case, thank you very much for your reaction so far!
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58313

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Hey guys,

I managed to do what I wanted, it was very simple. So simple that even I understand it :-) I assume you both know already how to do this, but I'm just posting my solution in case anybody else ends up here looking for the same.
	$(document).ready(function(){
          document.getElementById('answer68955X75X14261-1').checked= true;
          document.getElementById('answer68955X75X14262-1').checked= true;
          document.getElementById('answer68955X75X14263-1').checked= true;
          document.getElementById('answer68955X75X14264-1').checked= true;
          document.getElementById('answer68955X75X14265-1').checked= true;
          document.getElementById('answer68955X75X14266-1').checked= true;
          document.getElementById('answer68955X75X14267-1').checked= true;
	});

So, basically, you take the SGQA, add a dash and the answer option you want selected, and you 'check' it.
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58314

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Question. Can you have pieces of code for this "$(document).ready(function(){})" thing in several questions? Or would you then have to collect all code in one question, or say in the Group Description?
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58315

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Can you have pieces of code for this "$(document).ready(function(){})" thing in several questions?
You can have as many as yo like. They will just fire sequentially.
So, basically, you take the SGQA, add a dash and the answer option you want selected, and you 'check' it.
Yep, that will work but if you want to allow respondents to return to the page, you may want to throw in a test for checked radios:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qID = QQ;
 
		// Loop through the rows and check the first radio if none are already checked
		$('#question'+qID+' table.question tbody tr').each(function(i) {
			if($('input.radio:checked', this).length == 0) {
				$('input.radio:eq(0)', this).attr('checked', true);
			}
		}); 
	});
 
</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.

How to change default value in array 5 years 8 months ago #58316

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Can you have pieces of code for this "$(document).ready(function(){})" thing in several questions?
Another thought on this - it's not a bad idea to put all scripts in the group description anyway so they don't appear in emailed results or the data.
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.

How to change default value in array 5 years 8 months ago #58317

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Ah, your code is much better indeed - thank you!

And your point re: putting scripts in the group description is a good idea. My questions tend to be pretty long already (i.e. because of explanations etc), so leaving out the code would be wise.

Thank you very much!!!
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58318

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Yeah, cool - your code works :-)

So, do I understand correctly that if you would want the third option to be select, you'd change the '0' in
$('input.radio:eq(0)', this).attr('checked', true);
to '2'?
The administrator has disabled public write access.

How to change default value in array 5 years 8 months ago #58330

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Correct - the index starts at 0 so the first element is :eq(0), the second is :eq(1), etc...
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.

How to change default value in array 5 years 8 months ago #58341

  • Matherion
  • Matherion's Avatar
  • Offline
  • Gold Donor
  • Posts: 18
  • Karma: 1
Great, thanks!
The administrator has disabled public write access.
Time to create page: 0.386 seconds
Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form