Populate a hiddent numeric question with a random number?

More
5 years 3 weeks ago #86008 by guelphpeter
guelphpeter created the topic: Populate a hiddent numeric question with a random number?
Hi everyone,
I need to geneate a random number, to use in determining which of 4 possible scenarios should be presented to a respondent. I think the best way to proceed is to populate a hidden numeric question with a random number, and then use conditions to show the coresponding scenario, testing on the hidden question's value.

However I don't know how one could populate the hidden numeric question with a random number - I suspect java script would be involved.

I'd be grateful for any advice / help on a java script to populate a hidden numeric question with a random number between say 1 and 10.

We're running 1.91+

Thanks in advance for any help,

Peter

Please Log in to join the conversation.

More
5 years 3 weeks ago #86092 by tpartner
tpartner replied the topic: Populate a hiddent numeric question with a random number?
1) Set up your survey to use JavaScript .

2) Create your short-text question but do not hide it with the advanced question setting.

3) Add the following script to the source of the question. Replace "QQ" with the question ID .
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var randNum = Math.floor(Math.random()*10 + 1);
		$('#questionQQ').hide();
		$('#questionQQ input.text').val(randNum);
	});
 
</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 3 weeks ago #86125 by guelphpeter
guelphpeter replied the topic: Populate a hiddent numeric question with a random number?
Thank you very much for your help - greatly appreciated !

Peter

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now