Check out the LimeSurvey source code on GitHub!

Pre-filling answer text for questions

More
5 years 6 months ago #64888 by cbc2132
Hi -

I'm trying to pre-fill the answer text to a question in v1.91. I've tried every variation of javascript I can think of but nothing seems to work.

For example -

For a question, I've included the below snippet in it's question field. I've also tried to include the javascript in a separate question on a previous page, but that hasn't worked either.

i.e.

<script type="text/javascript">
document.getElementById("answer496692207").value = 'test';
</script>

What am i doing wrong? Please help!

Thanks!
Cadran

Please Log in to join the conversation.

More
5 years 6 months ago #64890 by tpartner
The ID doesn't look right - you're missing the "X"s in the SGQA. The ID should be like "answer{SID}X{GID}X{QID}".

Also, I don't think you should do anything until the page is fully loaded.

Something like:
<script type="text/javascript" charset="utf-8">
 
	// Wait for the DOM to load
	$(document).ready(function() {
 
		document.getElementById("answer49669X22X07").value = 'test';
 
	});
 
</script>

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 6 months ago #64892 by cbc2132
Thank you for the response!

Yes I didn't give a proper example SGQA, but the one I'm using in my code is correct. I tried making the change you suggested, but no luck.


If i would like answer49669X22X07 to display 'test', then do I include the javascript in Question 49669X22X07, or somewhere else?

thanks!

Please Log in to join the conversation.

More
5 years 6 months ago #64894 by tpartner
The script needs to go in the source of a question or group description on the same page as the element(s) you are trying to manipulate.

Did you turn off the XSS filter? Are there any JS errors? Can you get a simple JS alert to work?

Try jQuery:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		$('#answer49669X22X07').val('Your inserted value');
 
	});
 
</script>

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 6 months ago #64905 by cbc2132
Ok - that's where I've been putting the script. The XSS filter is off. I'm not receiving and errors and I'm able to run other scripts, such as alerts and adding text to the question body. For some reason though, adding text the the answer field is not working. Thanks!

Please Log in to join the conversation.

More
5 years 6 months ago #64907 by cbc2132
the jquery worked! thanks

Please Log in to join the conversation.

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