I am trying to define a variable 'res' which will contain the value of the participant's response to a question. I have the following code which is working fine.
var res =Number(document.getElementById('QR~QID1').value);
However, I don't want to insert the question id manually for every question, but have the code directly pull the question id. I know var qid= this.questionId; returns the question id. But I can't seem to figure out the syntax for using this variable directly. Any help will be much appreciated!
You can use below code:
var qid = this.questionId;
var res = (document.getElementById('QR~'+qid+'').value);
thanks rondev !
I tried this and for some reason it still does not work.
I am using this to save the participant response in an embedded data. I am running the following : am I doing anything obviously wrong?
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
if(type == "next")
{
qid=this.questionId;
var res =Number(document.getElementById('QR~'+qid+'').value);
Qualtrics.SurveyEngine.setEmbeddedData( "res", res );
}
});
I should add that if I hard code in the QID (e.g QR~QID2), it works.
For some reason it cannot retrieve value when it has to calculate the question id. I should also add that I have also tried defining a new variable equal to what you suggested in the bracket. And that variable correctly outputs QR~QID2. So your syntax is definitely correct. But if I insert that variable inside getElementById(var).value it does not update res.
thanks for your help again!
I have tested below code and working fine
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
var qid = this.questionId;
Qualtrics.SurveyEngine.setEmbeddedData("ED1", Number(document.getElementById('QR~'+qid+'').value));
}
});
Thanks that worked perfectly!!
I have a follow up question (let me know if I should post this as a new thread-- I'm new to the forum so not sure about all the rules).
My code is slightly more complicated - to verify the response I need to create a custom next button, and put in the code when that custom button is clicked, and not the usual addonPageSubmit. My code is below.
It works fine as long as I comment out Qualtrics.SurveyEngine.setEmbeddedData("res", Number(document.getElementById('QR~'+qid+'').value));
With that in, it has the same problem as before, it does not update the embedded field. (I can hard code it as QR~QID22 and all is well again). Do you think it is because of the custom button function?
Sorry for the many questions, and thanks very much for your time!
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery('#CustomNextButton').on('click', function() {
var res =Number(document.getElementById('QR~QID22').value);
var qid = this.questionId;
Qualtrics.SurveyEngine.setEmbeddedData("res", Number(document.getElementById('QR~'+qid+'').value));
if (res ==correct) {
jQuery('#NextButton').click();
jQuery('#CustomNextButton').hide();
} else {
/* This is where you set your error message text */
var errorMsg = "Please enter the correct code";
var x = document.createElement("DIV");
var t = document.createTextNode(errorMsg);
x.className = "custom-warning";
x.appendChild(t);
document.getElementById('Questions').parentElement.appendChild(x);
jQuery('.custom-warning').css("background", "pink");
jQuery('.custom-warning').css("color", "red");
jQuery('.custom-warning').css("font-size", "12px");
}
});
});
Need to change only one line. Below is the updated code:
Qualtrics.SurveyEngine.addOnReady(function()
{
var qid = this.questionId;
jQuery('#CustomNextButton').on('click', function() {
var res =Number(document.getElementById('QR~'+qid+'').value);
Qualtrics.SurveyEngine.setEmbeddedData("res", Number(document.getElementById('QR~'+qid+'').value));
if (res ==correct) {
jQuery('#NextButton').click();
jQuery('#CustomNextButton').hide();
} else {
/* This is where you set your error message text */
var errorMsg = "Please enter the correct code";
var x = document.createElement("DIV");
var t = document.createTextNode(errorMsg);
x.className = "custom-warning";
x.appendChild(t);
document.getElementById('Questions').parentElement.appendChild(x);
jQuery('.custom-warning').css("background", "pink");
jQuery('.custom-warning').css("color", "red");
jQuery('.custom-warning').css("font-size", "12px");
}
});
});
Thank you sooo much! works like a dream now!
much appreciated!
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.