Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: javascript prefill losing answer on previous

javascript prefill losing answer on previous 2 years 7 months ago #72398

  • badronald
  • badronald's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Karma: 0
I am using javascript to fill in 2 short texts with the answers from 2 multiple choice questions. It is working fine. My problem is that if the user clicks next, sees their answer then clicks previous and only changes the answer to one of the multiple choice questions the other question is considered 'blank' and does not get filled in.
Any help?
This is the script I am using.
script type="text/javascript" charset="utf-8"
$(document).ready(function() {

var q1ID = 21389;
var qHidden = 21394;

// Hide the hidden question


var q1Answers = new Array();

// Build an array of q1 answers checked
$('#question'+q1ID+' input.checkbox').click(function(){
var ansText = $(this).siblings('label').text();
if($(this).attr('checked') == true) { // Add to array if checked
q1Answers.push(ansText);
}
else { // Remove from array if unchecked
q1Answers = jQuery.grep(q1Answers, function(element) {
return element != ansText;
});
}
});

// Interrupt next/submit function
$('#movenextbtn, #movesubmitbtn').click(function(){

$('#question'+qHidden+' input.text').val(q1Answers);

return true;
});

});
/script
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72402

  • Steve
  • Steve's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 494
  • Thank you received: 46
  • Karma: 17
Hello,

where is this scipt used/located? In the template.js file or within a pstpl page?

I'm not sure but perhaps the reason it doesn't work is that the array q1Answers is not being filled if you don't do anything (i.e. if the click event isn't happening). So "var q1Answers = new Array();", click event doesn't happen and after you click "next" "val(q1Answers)" doesn't have anything in it.
Mit freundlichen Grüßen/Best regards,

Stefan Gohlke
LimeSurvey Team
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72403

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5324
  • Thank you received: 294
  • Karma: 248
When is the Javascript called? When clicking next/submit?

It might also depend on the $deletenonvalues setting. I'm not sure though but it might be worth testing, see docs.limesurvey.org/Optional+settings&st...rvey#Survey_Behavior

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72421

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4133
  • Thank you received: 765
  • Karma: 347
I think vierundzwanzig may be correct. try doing everything in the "Next" button click event:
$(document).ready(function() {
 
	var q1ID = 21389;
	var qHidden = 21394;
 
	// Interrupt next/submit function
	$('#movenextbtn, #movesubmitbtn').click(function(){
 
		var q1Answers = new Array();
 
		// Build an array of q1 answers checked
		$('#question'+q1ID+' input.checkbox:checked').each(function(i){
 
			var ansText = $(this).siblings('label').text();
 
			q1Answers.push(ansText);
		});
 
		$('#question'+qHidden+' input.text').val(q1Answers);
 
		return true;
	});
 
});
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72423

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
badronald-

There is probably a way to do what you want with Expression Manager. Why are you trying to pipe answers from one question into short text fields?
(1) To implement the equivalent of "default" values for short text
(2) To display that other value and have the user respond to it (e.g. by filling out another question)
(3) To store a computation in a field so that it will be saved in the database?
(4) Other (and if so, please clarify.

/Tom
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72425

  • badronald
  • badronald's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Karma: 0
I need to save the choice they select on the same page and on the next page pipe it in to ask questions about it. I also need to save the choice as text into the database. I also have a script I am running which takes all the values they do not select from the 2 multi choice questions and randomly selects a value for a third hidden short text field.

I need to display these three choices to the responder.
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72426

  • badronald
  • badronald's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Karma: 0
the script is in the source code of the short text question
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72429

  • badronald
  • badronald's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Karma: 0
When I tried the script above it did not work at all.
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72431

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4133
  • Thank you received: 765
  • Karma: 347
Well, I'm not clear on what you are trying to achieve. What questions are on what pages and what is the exact behaviour supposed to be?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: javascript prefill losing answer on previous 2 years 7 months ago #72433

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
badronald-

It sounds like you're still trying to support the features you discussed in this thread. If so, please try the 1.92 solution to see if it works for you.

/Tom
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.152 seconds
Donation Image