javascript prefill losing answer on previous

More
5 years 4 months ago #72398 by badronald
badronald created the topic: javascript prefill losing answer on previous
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

Please Log in to join the conversation.

More
5 years 4 months ago #72402 by Steve01
Steve01 replied the topic: javascript prefill losing answer on previous
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

Please Log in to join the conversation.

More
5 years 4 months ago #72403 by Mazi
Mazi replied the topic: javascript prefill losing answer on previous
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)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 4 months ago #72421 by tpartner
tpartner replied the topic: javascript prefill losing answer on previous
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.

Please Log in to join the conversation.

More
5 years 4 months ago #72423 by TMSWhite
TMSWhite replied the topic: javascript prefill losing answer on previous
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

Please Log in to join the conversation.

More
5 years 4 months ago #72425 by badronald
badronald replied the topic: javascript prefill losing answer on previous
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.

Please Log in to join the conversation.

More
5 years 4 months ago #72426 by badronald
badronald replied the topic: javascript prefill losing answer on previous
the script is in the source code of the short text question

Please Log in to join the conversation.

More
5 years 4 months ago #72429 by badronald
badronald replied the topic: javascript prefill losing answer on previous
When I tried the script above it did not work at all.

Please Log in to join the conversation.

More
5 years 4 months ago #72431 by tpartner
tpartner replied the topic: javascript prefill losing answer on previous
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.

Please Log in to join the conversation.

More
5 years 4 months ago #72433 by TMSWhite
TMSWhite replied the topic: javascript prefill losing answer on previous
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

Please Log in to join the conversation.

More
5 years 4 months ago #72437 by badronald
badronald replied the topic: javascript prefill losing answer on previous
I have doing it via expressions manager but had no luck and little time. :(

Here is a link to a version
strangereaction.com/jenn/limesurvey/inde...58&newtest=Y&lang=en

Please Log in to join the conversation.

More
5 years 4 months ago #72438 by TMSWhite
TMSWhite replied the topic: javascript prefill losing answer on previous
If you attach the first group from that survey (where you are trying to collect the response and save it), I can try to help make it work in 1.92.

Please Log in to join the conversation.

More
5 years 4 months ago #72441 by badronald
badronald replied the topic: javascript prefill losing answer on previous
thank you!
Attachments:

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now