Displaying a random number within a question | XM Community
Skip to main content

Hi All,
I am trying to do the following with the question below -- create a random generator to include the ranges as following $50, $100, $200, $400, $800 -- in the question below:
Based on the selected level of support, would you be willing to pay $50 per hour?
This would need to be done Javascript but unclear how to proceed with the code -- any help or advice is greatly appreciated!
many thanks for your time,
Jeremy

HTML:
Based on the selected level of support, would you be willing to pay  per hour?
onload JS:
var dollars = ="$50","$100","$200","$400","$800"];
jQuery("#dollars").text(dollarsrparseInt("${rand://int/0:4}")]);


Is there a way to do this without java script ?



https://www.qualtrics.com/community/discussion/comment/38093#Comment_38093You could use a survey flow randomizer and set an embedded data field to one of five values, then pipe that embedded data field into your question.


https://www.qualtrics.com/community/discussion/comment/38121#Comment_38121OK thanks, but can you help me a little more maybe by saying me step by step how to do it, sorry im New in qualtrics.
Please


@TomG, I tried to paste your code to my survey but it does not work. I think for the javacode, you miss a "; but for the content put in the HTML view, I have no idea why it does not work. Could you please share with me some hints?


https://community.qualtrics.com/XMcommunity/discussion/comment/40455#Comment_40455The JS above was missing a quote. I corrected it.


TomG Thanks! I used the corrected JS but when I paste the HTML content, it does not show from the normal view. Anything I did wrong?
qualtrics 2.JPGqualtrics 3.JPG
qualtrics.JPG


https://community.qualtrics.com/XMcommunity/discussion/comment/40458#Comment_40458That's correct, the "dollars" span will be empty until the JS runs. You can put something inside the span if you want - it will be replaced by the JS. Since you didn't include a $ sign with the numbers in your array you might want add one before the span. As it is now, it will be something like "...willing to pay 1 per hour?"


TomG Many thanks for the kind explanation. I am totally new but how can I have the JS run?


https://community.qualtrics.com/XMcommunity/discussion/comment/40460#Comment_40460Sorry for the further bothering but why can't I the "dollars" (1/2/3/4/5) display on the normal view? In my case, it seems that the dollars span does not work.


https://community.qualtrics.com/XMcommunity/discussion/comment/40462#Comment_40462Pardon me TomG I figured it out. For others new to JS, the script will run when you actual "load" the page (e.g. preview the question)


https://community.qualtrics.com/XMcommunity/discussion/comment/40463#Comment_40463The JS will run when you preview the question, block, or survey.


https://community.qualtrics.com/XMcommunity/discussion/comment/40473#Comment_40473Many thanks TomG !!!Can I ask a follow-up question? I am trying to export the individual participant's answer as a pdf file. I notice that the text I wrote with JS does not show up if I print out the survey (in pdf). Is there a way to take care of this problem? I appreciate the kind help a lot!!


This is pretty easy and doesn't need any JS at all.
image.pngand then simply put the embedded data in the question.
image.png


https://community.qualtrics.com/XMcommunity/discussion/comment/40477#Comment_40477Add 'randDollars' as an embedded data field to the survey flow, and modify the JS like this:
var dollars = ["$50","$100","$200","$400","$800"];
randDollars = dollars[parseInt("${rand://int/0:4}")];
jQuery("#dollars").text(randDollars);
Qualtrics.SurveyEngine.setEmbeddedData("randDollars",randDollars);


Leave a Reply