How to get each elements of a dropdown question with jQuery | XM Community
Skip to main content

How to get each elements of a dropdown question with jQuery

  • August 22, 2022
  • 7 replies
  • 742 views

JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • QPN Level 2 ●●

Hi there,
In that question, I'm trying to get each elements of the two dates with an addonpagesubmit JS :
Capture d’écran 2022-08-22 à 15.15.37.pngI'm using :
jQuery("#"+this.questionId+" option:selected").text();
But by this way I get a serie of numbers for the two date. How can I get each element separalty ? (I know the ID of each one like QR~QID67#1~4 for the day of the first row ; 2~4 ;3~4...)
Thank you

7 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 22, 2022

Loop through the selected options:
jQuery("#"+this.questionId+" option:selected").each(function() {
console.log(jQuery(this).text()); //log the text for each selection
});


JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 22, 2022

Thank you TomG (again and again 😉) but I think I don't know how to log the text as embedded data for each selection after this script ?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 22, 2022

I'm not sure how you want save it, but this would save each in its own embedded data field:
jQuery("#"+this.questionId+" option:selected").each(function(i) {
  Qualtrics.SurveyEngine.setEmbeddedData("selected"+(i+1),jQuery(this).text());
});
If you want to combine the first row and second row into single values, you could do some branching based on the modulus of i.


JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 22, 2022

I just want to build a var date() var with each elements in columns (like DD+"/"+MM+"/"+YYYY)


JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 22, 2022

I apologize for contacting you again but I'm really stuck on my question
I manage to make your loop work with the embedded data but I can't "translate" it to directly use the variables in my javascript code.
FYI here is the JS code I used with piped text but now I have to build the var "end" onSubmit thats why the piped text can't works.
image.png


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 22, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/48676#Comment_48676You can't set and pipe embedded data fields on the same page because the pipes get resolved on the server before the page is sent to the browser. The good news is you don't need to. To simplify, let's say you have just one row in your side-by-side (3 drop downs - day, month, year). Then:
var dateParts = [];
jQuery("#"+this.questionId+" option:selected").each(function(i) {
  dateParts.push(jQuery(this).text());
});
var end = new Date(dateParts.reverse().join("-"));


JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 23, 2022

Wonderful !
To get only the first row, I used an
if(i >= 0 && i <=2)
I don't know if it was the best way to get that but It works :)
Tks