Question Javascript doesn't work when I move it into a new survey | XM Community
Skip to main content
Solved

Question Javascript doesn't work when I move it into a new survey

  • 10 July 2024
  • 3 replies
  • 52 views

Hi everyone,

 

I have the JS below in a question and it works fine in the original survey, but when I move the question into another survey, the Javascript still shows up in the survey builder but it doesn’t work in preview.

 

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/

});


Qualtrics.SurveyEngine.addOnReady(function() {
    
    var text_placeholder = 'Good things or people you can feel grateful about, and why.'
    
    jQuery("#question-"+this.questionId+" textarea").attr("placeholder", text_placeholder);
    
});

Qualtrics.SurveyEngine.addOnUnload(function()
{
    /*Place your JavaScript here to run when the page is unloaded*/

});

 

Thanks for your attention,

Benji

3 replies

Userlevel 3
Badge +13

@benji7000 

The only thing i can think off is that question ID can be different in other surveys.

Userlevel 5
Badge +17

@benji7000 Please check how the question is set up in your other survey. Is it specified with exactly the same details? 

The only issue I see is this part: 

jQuery("#question-"+this.questionId+" textarea") 

Usually the IDs do not start with “question” but rather looks like this:

You can check the developer tools of your browser (usually F12) to find out the ID of the text area and then do something like:

var textarea = document.getElementById("myTextareaId");

If you only have one textarea HTML element on the page, you can also use (takes the first textarea element on the page):

var textarea = document.querySelector("textarea");

Putting everything together… Try this: 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});


Qualtrics.SurveyEngine.addOnReady(function() {

var text_placeholder = 'Good things or people you can feel grateful about, and why.'
var textarea = document.querySelector("textarea");
if (textarea) {
textarea.setAttribute("placeholder", text_placeholder);
}

// jQuery("#question-"+this.questionId+" textarea").attr("placeholder", text_placeholder);

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

For me it correctly sets the placeholder:

Best
Christian

Userlevel 7
Badge +39

The below should work for all layouts other than the simple

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var text_placeholder = 'Good things or people you can feel grateful about, and why.'

jQuery("#"+this.questionId+" .InputText").attr("placeholder", text_placeholder);
});

 

Leave a Reply