JavaScript applying to multiple questions in a block instead of just the target question | XM Community
Skip to main content

Hello,

 

I am building a simple survey to collect questions for an FAQ. I have two questions - Q1. Submit questions from a team member’s perspective and Q2. Submit questions related to a manager's perspective.

I am using the form field type so responders can input multiple questions and have each question in a separate field.

Utilizing the JavaScript below, copied to both questions, I am able to add a button that allows a responder to add additional fields to submit additional questions. However, I’d like the code to function independently for each question. Right now, during preview, if I click ‘add field’ under question 1 it also adds a field under question 2 and the ‘add field’ under question 2 doesn’t work. It works perfect if I do a page break, but I’d like the questions to be on the same page. Any help would be wonderful!

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery("#"+this.questionId+" td:not(.ControlContainer)").hide();

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
 var cs = jQuery("#"+this.questionId+" .ChoiceStructure");
    cs.find("tr:not(:lt(1))").hide();
    cs.append("<input type='button' id='add' value='Add Question' name='+' />");
    jQuery("#add").on('click',function(){
        var c = cs.find("tr:visible").length;
        cs.find("tr:eq("+c+")").show(); 
    });

});

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

});

If you have multiple questions on the page you'll need to give unique ids to each add button. 

For example where it says id='add' change that to add1, add2 etc.

And same for jQuery("#add") 


If you have multiple questions on the page you'll need to give unique ids to each add button. 

For example where it says id='add' change that to add1, add2 etc.

And same for jQuery("#add") 

Thank you so much for your help! This worked perfectly.


Leave a Reply