Hi everyone!
I'm trying to set embedded data so that I replace apostrophes ( ' ) in a text response with a backslash apostrophe ( \\' ) using javascript.
I was able to figure out the code itself. Here's what I have:
var first = "${q://QID2/ChoiceTextEntryValue/1}";
var first_mod = first.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("first", first_mod);
var last = "${q://QID2/ChoiceTextEntryValue/2}";
var last_mod = last.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("last", last_mod);
Ideally, I'd set this in the question itself. However, I've tried including it under the header, "Qualtrics.SurveyEngine.addOnUnload" and "Qualtrics.SurveyEngine.addOnPageSubmit" based on a few other help pages I've found. In all cases, the embedded data values are not set. (Help pages I based this on are here, here, and here for reference.)
I've only been able to get it to work by putting the above code under the header "Qualtrics.SurveyEngine.addOnload" on a completely different question on the next page, which is not ideal.
My question is: How can I set the embedded data through javascript on the actual question, itself? I'd really rather not have the javascript associated with a completely different question. I've attached some screenshots, in case that's helpful. Let me know if there's anything I can clarify.
Piped values are resolved before the page is sent to the browser, so you can't pipe an answer to a question on the same page as the question - it will be empty. You need to retrieve the answer values directly from the DOM (page) using JS.
You should use addOnPageSubmit. addOnUnload won't work.
Thanks Tom. I got it to work with the following code:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var first = jQuery("#QID2 input[type=text]").eq(0).val();
var first_mod = first.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("first", first_mod);
var last = jQuery("#QID2 inputttype=text]").eq(1).val();
var last_mod = last.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("last", last_mod);
});
I was wondering if you knew what input type I'd use if I wanted to do something similar for a dropdown question? I've tried inputttype=dropdown] and inputttype=select] which both do nothing. I've also tried the following:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
var account = jQuery("#QID58 select").val();
var account_mod = account.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("account", account_mod);
});
This just feeds the option number into embedded data, not the actual option itself. Any advice would be appreciated.
Hi Tom, nevermind, I found the answer in a different question (from you)! So thanks for all your help.
For anyone wondering, here's the solution to get dropdown options to send to embedded data through JS:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
var account = jQuery("#QID58 option:selected").text();
var account_mod = account.replace(/'/g, "\\\\'");
Qualtrics.SurveyEngine.setEmbeddedData("account", account_mod);
});
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.