Why javascript works in separate pages but doesn't work if I put the two questions in the same page? | XM Community
Skip to main content
Solved

Why javascript works in separate pages but doesn't work if I put the two questions in the same page?

  • October 23, 2020
  • 2 replies
  • 136 views

I intend to insert a random text into my question body. The random text could be either "positive" or "negative". Therefore,
(1) I create an embedded data named "tresult1" in the very beginning of the survey.
(2) I insert where I would like the random text inserted in the html view of the question body.
(3) add the follow javascript code in the question.

Qualtrics.SurveyEngine.addOnReady(function() {
var tresults = ["positive", "negative"];
var i = "${e://Field/tresult1}";
var tresult = tresults[i];
document.getElementById("random").innerHTML = tresult;
});
I found that the random text can be successfully inserted if I place only one of such questions on each page. But if I place more than one questions, then the second question and any questions following will not have the random text inserted. The only way to make the javascript work is to add page break. After add page break, each question will work as expected.
I would like to place two questions on each page. Does anyone know how to solve this problem? Thanks a lot!

Best answer by TomG

An element id has to be unique. To combine more than one on a page, use a class instead of an id.

2 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • October 23, 2020

An element id has to be unique. To combine more than one on a page, use a class instead of an id.


  • Author
  • October 23, 2020

https://www.qualtrics.com/community/discussion/comment/31383#Comment_31383Thank you for your prompt reply! It really helps!