How to display one of two images based on even/odd number input from an earlier question? | XM Community
Skip to main content
Question

How to display one of two images based on even/odd number input from an earlier question?

  • January 17, 2025
  • 9 replies
  • 79 views

Hello! 

I’m working on a survey where users input a participant ID, and I’d like for Image A to be displayed for users with an even ID and Image B to be displayed for users with an odd ID. It doesn’t matter to me whether the images are on a new page/in a new block, so long as they are able to continue on with the rest of the survey after seeing the image. 

My issue: Every time I preview the survey, it goes directly from the ID question to the Passcode block, skipping over the two blocks (2 and 3) I currently have for the images.

 

Here’s my JavaScript on the Participant ID question:

Qualtrics.SurveyEngine.addOnload(function()

{

    /*Place your JavaScript here to run when the page loads*/

});

 

Qualtrics.SurveyEngine.addOnReady(function()

{

    /*Place your JavaScript here to run when the page is fully displayed*/

    // Get the value of the numeric input

    var userInput = "${q://QID52/ChoiceTextEntryValue}";

    var num_id = parseInt(userInput, 10);

 

    if (num_id % 2 == 0) {

        // If even, set a new embedded data variable to "even"

        Qualtrics.SurveyEngine.setEmbeddedData("ppt_id", "even");

    } else {

        // If odd, set a new embedded data variable to "odd"

        Qualtrics.SurveyEngine.setEmPPbeddedData("ppt_id", "odd");

    }

});

 

Qualtrics.SurveyEngine.addOnUnload(function()

{

    /*Place your JavaScript here to run when the page is unloaded*/

})

 

And below is my current survey flow:

 

I have also tried not setting a specific value for ppt_id in the survey flow, changing the order of the blocks, and other such variations. I don’t want to have to write conditionals for every possible ID, and was wondering if there’s an easier way to do this.

Thank you!

9 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • January 18, 2025

All question answers and embedded data need a page break to populate. In your case, you have set the JavaScript to set the value ppt_id, even before the system knows what the value of QID52 is, because you are using it in the same question. 

Furthermore, the order matters a lot. In your survey, the value of ppt_id is being set to the answer input in QID52, after you’ve used the JS. Therefore, it won’t be either even or odd, it will just be the number. You can check this by going into your data and seeing the value of ppt_id.

 

Start by declaring ppt_id at the top, but don’t give it a value, leave it empty.

There are multiple ways to go about this, here are three. One is to use javascript, triggered on every keystroke and update the value of ppt_id based on your conditions. This JS will need to be used in the same question.

 

Second, you could use your existing javascript, but in a different question. Place this question after QID52, with atleast a page break in between. And have the branches after this question.

 

Third, you could use a branches to match a particular regex. This is the regex for detecting (loosely) an odd number: 

^.+[13579]$ 

  • ^ indicates the start of your string and $ this indicates the end.
  • .+ indicates that it should match any number of any character (spaces, numbers, letters etc.)
  • [13579]$ indicates that the last character should be one of them

You can read more online to make your requirements more strict.


  • Author
  • 4 replies
  • January 20, 2025

Hi Ahmed, I tried following your third suggestion with regex but am still running into the same problem where the survey skips from the ID block to the Passcode block after the regex branches. Is there a way to troubleshoot this? Below is my survey flow:

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5932 replies
  • January 20, 2025

@Eng,

Your original script is pretty close.  You just need to change the function name and get the value from the input field. Try this:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
    // Get the value of the numeric input
    var userInput = jQuery(this.questionContainer).find("[type=text]").val();
    var num_id = parseInt(userInput, 10);

    if (num_id % 2 == 0) {
        // If even, set a new embedded data variable to "even"
        Qualtrics.SurveyEngine.setEmbeddedData("ppt_id", "even");
    } else {
        // If odd, set a new embedded data variable to "odd"
        Qualtrics.SurveyEngine.setEmbeddedData("ppt_id", "odd");
    }
});

 


  • Author
  • 4 replies
  • January 20, 2025

@TomG

The survey now displays an image, but it only ever displays the image in the block under the odd branch in the Survey Flow. Is there a logical inconsistency with my survey flow and the code? I’m working with the flow I most recently shared in the replies with the conditions changed to “Is Equal to odd” and “Is Equal to even”. 

Thanks!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5932 replies
  • January 20, 2025
Eng wrote:

@TomG

The survey now displays an image, but it only ever displays the image in the block under the odd branch in the Survey Flow. Is there a logical inconsistency with my survey flow and the code? I’m working with the flow I most recently shared in the replies with the conditions changed to “Is Equal to odd” and “Is Equal to even”. 

Thanks!

It works for me.  Is the script attached to Q52?  I don’t think you need Q54.


  • Author
  • 4 replies
  • January 20, 2025

@TomG, I believe so. I should just be pasting your code into the JS section of the ID question, correct? 

I just tried this again with a blank survey and ran into the same problem where it only displays the image from the odd block no matter the number I put in. Below is the new survey:

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5932 replies
  • January 20, 2025

Are you using Simple layout (aka New Survey Experience)?


  • Author
  • 4 replies
  • January 20, 2025

@TomG I did not previously have it enabled, but I just tried with it turned on and it skipped over the even/odd blocks entirely.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5932 replies
  • January 20, 2025
Eng wrote:

@TomG I did not previously have it enabled, but I just tried with it turned on and it skipped over the even/odd blocks entirely.

The reason I asked was because your script won’t work with Simple layout as currently written.

I’m not sure your issue is because the script works correct for me.


Leave a Reply