Using Javascript to get slider value and save to embedded data | XM Community
Skip to main content

Hi everyone,
I'm using a slider question to record a participant's willingness to pay for a certain product as part of an economics experiment. This WTP value will be compared to a randomly generated value in the survey flow. Unfortunately I've not been able to extract the value from the slider and set it to an embedded data field.
My JS so far on the slider question is:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var randomNum = (Math.random() * (0.21 - 0.0) + 0.0).toFixed(2);
Qualtrics.SurveyEngine.setEmbeddedData("WTP_Random", randomNum);
console.log(randomNum);
});
Qualtrics.SurveyEngine.addOnReady(function()
{
var value;
var that = this.questionId;
jQuery(document).on('change',function(){
value=parseInt(jQuery("[id='"+that+"~1~result']").val());
});
console.log(value);
Qualtrics.SurveyEngine.setEmbeddedData("WTP_Subject", value);
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});

Any help would be greatly appreciated.
Thanks, Chris

Use the below code:
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
Qualtrics.SurveyEngine.setEmbeddedData("WTP_Random",(Math.random() * (0.21 - 0.0) + 0.0).toFixed(2));
Qualtrics.SurveyEngine.setEmbeddedData("WTP_Subject", jQuery("#"+this.questionId+" .ResultsInput:eq(0)").val());
}
});

Assuming you have single choice in your slider question.


https://www.qualtrics.com/community/discussion/comment/29328#Comment_29328Thanks rondev, could you tell me where I should put this in the default brackets (eg "addOnLoad" or "addOnReady"? And is there any way to display the output while previewing a question? I have a lot of these single slider questions so would like to check before pasting it into all of them. Thanks a lot!


You will paste the code outside all the default brackets, at the end, as shown below
image.pngAdd a page break after this question and then add a dummy descriptive question. Pipe both the embedded data in this dummy question.


https://community.qualtrics.com/XMcommunity/discussion/comment/29328#Comment_29328Hi rondev,
How can we set the value on click?


Leave a Reply