CODE TO REVERSE HOW NAMES FROM DROPDOWN LISTS ARE DISPLAYED ON SUBSEQUENT PAGES OF SURVEY | XM Community
Solved

CODE TO REVERSE HOW NAMES FROM DROPDOWN LISTS ARE DISPLAYED ON SUBSEQUENT PAGES OF SURVEY

  • 17 February 2021
  • 3 replies
  • 12 views

Userlevel 1
Badge

Hello, Everyone:
On page 1 of a project, I've created a dropdown menu that includes a list of names (Last Name, First Name) of the select persons who have been targeted as respondents. EXAMPLE: "Doe, John" (Intentional formatting of names for ease in data analysis later.)
On page 2, after respondents have chosen their names from the dropdown list on the previous page, I'd like to them to be greeted with a, "Hello (First Name Last Name)!" statement that reverses the format of the names from the dropdown list. EXAMPLE: "Hello, John Doe!"
I was someone would know of and be willing to share code that would reverse the display the respondents' names from the dropdown list when they appear in greeting statement on page 2. I have no idea whether what I'd like to do is possible, but I'm living with a, "there is code for everything", philosophy at the moment that I'm putting to the test. 😊
Thank you in advance for your help.

icon

Best answer by ahmedA 17 February 2021, 15:03

View original

3 replies

Userlevel 7
Badge +21

You can get this done without custom coding.
For the question you want to display the reversed name, create a loop and merge block. Select loop off a question. Now your field one will be populated by all the names in your dropdown list. For field 2 and field 3, enter the first and last name respectively.
In the question text, pipe the L&M fields. So it will something like

Hello ${LMField/2} ${LMField/3}

Since the respondent will select only one option, it'll only be displayed once.

Hi!
I found a solution for this:

  1. On your second page you insert a question with just a text box.

  2. In the Rich Content Editor you insert a pipe text and choose the user's answer to the question on page 1 as your content.

  3. Now your text on page 2 should display "Lastname, Firstname" (check it out in the preview).

  4. In the preview you can now inspect (dev tools --> inspect, depends on browser) the html elements and find your text box. This element is "myElement" in the below code. You might have to change the ClassName in the code below or find another way to pick the element.

  5. Now you add the following code as custom JavaScript code for the question on the second page (click on the questions settings --> "Add JavaScript"). Add the code in the "addOnReady" function.


myElement = document.getElementsByClassName("QuestionText BorderColor")[0];
myString = myElement.innerHTML;
myArray = myString.split(',');
myElement.innerHTML = "Hello " + myArray[1] + " " + myArray[0] + "!";

Hope this helps!
BR, Nico

Userlevel 1
Badge

AmedA, works great! Thank you so much again.

BR, Nico, thank you, too. I should have been more specific about not having permissions to insert JavaScript into Qualtrics. Completely my fault, and I'm sorry


Leave a Reply