Welcome, Guest
Username: Password: Remember me

TOPIC: Populate a hiddent numeric question with a random number?

Populate a hiddent numeric question with a random number? 1 year 10 months ago #86008

  • guelphpeter
  • guelphpeter's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 36
  • Thank you received: 1
  • Karma: 0
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
The administrator has disabled public write access.

Re: Populate a hiddent numeric question with a random number? 1 year 10 months ago #86092

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4115
  • Thank you received: 760
  • Karma: 346
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.

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: Populate a hiddent numeric question with a random number? 1 year 10 months ago #86125

  • guelphpeter
  • guelphpeter's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 36
  • Thank you received: 1
  • Karma: 0
Thank you very much for your help - greatly appreciated !

Peter
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.122 seconds
Donation Image