Hello,
I am trying to get the numerical value of a response (1 for first choice, 2 for second and so on) so I can use it to set a specific email.
Here is my code so far:
Qualtrics.SurveyEngine.addOnload(function()
{
document.getElementbyID("QID6").style.display="none";
this.questionclick = function(event,element)
{
console.log(event, element);
if (element.tagName == 'SELECT')
{
var choiceNum = element.value
if (choiceNum == 1) {
Qualtrics.SurveyEngine.setEmbeddedData("SBAEmail","email@email.com");
} else if (choiceNum == 2) {
Qualtrics.SurveyEngine.setEmbeddedData("SBAEmail","email2@email.com");
} else if (choiceNum == 3) {
Qualtrics.SurveyEngine.setEmbeddedData("SBAEmail","email3@email.com");
}
}
}
});
This only works for the first reponse. What am I missing here?
Page 1 / 1
You and @TeachMeQualtrics just asked the same question. You are trying to do it the hard way. Try this:
```
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var list = {
1: "emailAddress1",
2: "emailAddress2",
3: "emailAddress3"
};
Qualtrics.SurveyEngine.setEmbeddedData("SBAEmail", list[jQuery("#"+this.questionId+" select").val()]);
});
```
```
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var list = {
1: "emailAddress1",
2: "emailAddress2",
3: "emailAddress3"
};
Qualtrics.SurveyEngine.setEmbeddedData("SBAEmail", list[jQuery("#"+this.questionId+" select").val()]);
});
```
That looks much cleaner. Why use addOnPageSubmit over addOnload?
I> @maindanger said:
> That looks much cleaner. Why use addOnPageSubmit over addOnload?
>
It is cleaner and more efficient to set the embedded data field once instead of each time the question is clicked.
> That looks much cleaner. Why use addOnPageSubmit over addOnload?
>
It is cleaner and more efficient to set the embedded data field once instead of each time the question is clicked.
What does the "#" and "select" mean in the jQuery? Im not a programmer but Im trying to learn. THank you for your help.
> @maindanger said:
> What does the "#" and "select" mean in the jQuery? Im not a programmer but Im trying to learn. THank you for your help.
jQuery uses CSS-style selectors. # means it is an id (e.g., #QID1) and select is an html tag. So, the selector selects the select elements inside the current question.
> What does the "#" and "select" mean in the jQuery? Im not a programmer but Im trying to learn. THank you for your help.
jQuery uses CSS-style selectors. # means it is an id (e.g., #QID1) and select is an html tag. So, the selector selects the select elements inside the current question.
Tom, Thank you so so much for your help and time. We did not realize we were both posting! !
We have tried your code, entering the question query,
!
with a matching action, but are unable to make the action work. I tried just using QID40, or just 40. I know this must be something simple I am missing, but i spent all night trying to make it work.
Thank you, we owe you one for your assistance.
We have tried your code, entering the question query,
!
with a matching action, but are unable to make the action work. I tried just using QID40, or just 40. I know this must be something simple I am missing, but i spent all night trying to make it work.
Thank you, we owe you one for your assistance.
> @TeachMeQualtrics said:
> Tom, Thank you so so much for your help and time. We did not realize we were both posting! !
> We have tried your code, entering the question query,
> !
> with a matching action, but are unable to make the action work. I tried just using QID40, or just 40. I know this must be something simple I am missing, but i spent all night trying to make it work.
> Thank you, we owe you one for your assistance.
Remove QID40. The value of this.questionId is the question id (i.e., QID40). Also, the JS is written for a select (aka drop down). You'll have to change the selector if you use radio buttons:
```
jQuery("#"+this.questionId+" input:checked").val()
```
> Tom, Thank you so so much for your help and time. We did not realize we were both posting! !
> We have tried your code, entering the question query,
> !
> with a matching action, but are unable to make the action work. I tried just using QID40, or just 40. I know this must be something simple I am missing, but i spent all night trying to make it work.
> Thank you, we owe you one for your assistance.
Remove QID40. The value of this.questionId is the question id (i.e., QID40). Also, the JS is written for a select (aka drop down). You'll have to change the selector if you use radio buttons:
```
jQuery("#"+this.questionId+" input:checked").val()
```
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.