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

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

  • 23 October 2020
  • 2 replies
  • 89 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!

icon

Best answer by TomG 23 October 2020, 14:44

View original

2 replies

Userlevel 7
Badge +27

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

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

Leave a Reply