Javascript help please! | XM Community
Skip to main content

A while back, someone helped me to code the following question to only reveal the next row pending a certain response on the previous row.

This is the javascript:

Qualtrics.SurveyEngine.addOnload(function()
{
        quest = this;
    let qc = quest.questionContainer;
    let all_rows = qc.querySelectorAll(".Choice");
    for (let i = 1; i < all_rows.length; i++) {
        all_rowsi].hide();
    }
    let no_id =
        "." + qc.querySelectorAll(".q-radio").1].parentElement.className.match(/c\d+/gm).join();
    no_id = Number(
        qc
            .querySelector(no_id + ":not(th)")
            .querySelector("input")
            .id.split("~")3]
    );
    quest.questionclick = function (ev, el) {
        if (el.className) {
            let row = Number(el.id.split("~"){2]);
            let choice = Number(el.id.split("~")3]);
            if (choice == no_id && row < all_rows.length) {
                all_rows~row].show();
            } else if (choice == no_id && row == all_rows.length) {
                alert("That's a lot of sports/physical activity classes outside of school! Please use the text box on the next page to record any additional sports/physical activity classes your child participated in over the past year.");
            }
        }
    };/*Place your JavaScript here to run when the page loads*/

});

 

I’m rehashing this question for a new purpose but don’t know where/how to tweak the javascript to make it work properly.

The new question:

I have 10 rows available and when the answer ‘Yes’ is ticked for “Add another sport/activity?”, the next row appears (as planned). However, if I tick ‘Term 2’ under “When did they participate?” the next row appears then too!

How can I fix this please?

 

 

Since there are two choice 2’s in each row, in addition to checking the row and choice, you also need to check the column in your click handler.


@TomG thank you!

Do you know how I do that please? (I have no idea about how to write JavaScript)


@TomG thank you!

Do you know how I do that please? (I have no idea about how to write JavaScript)

Can you ask the person that created the JavaScript?


@ahmedA ...I’m not sure if you’re still around these forums but if you are, are you able to help me with this please?


@TomG thank you!

Do you know how I do that please? (I have no idea about how to write JavaScript)

Can you ask the person that created the JavaScript?

Hi @TomG,

I’ve tried contacting the person who created the JavaScript, via the forums and through DM but to no avail. Are you able to help me please? Even if to just advise where I need to tweak it?

Or anyone else?

Thanks,

Kate.


@KateD - The code you shared is overcomplicated. This should do it:

Qualtrics.SurveyEngine.addOnload(function() {
var rows = jQuery("#"+this.questionId+" tr.Choice");
rows.not(":first").hide();
rows.find("td.SBS5 inputtvalue=2]").click(function() {
var nextRow = jQuery(this).closest("tr").next("tr");
if(nextRow.length==0) alert("That's a lot of sports/physical activity classes outside of school! Please use the text box on the next page to record any additional sports/physical activity classes your child participated in over the past year.");
else nextRow.show();
});
});

 


@KateD - The code you shared is overcomplicated. This should do it:

Qualtrics.SurveyEngine.addOnload(function() {
var rows = jQuery("#"+this.questionId+" tr.Choice");
rows.not(":first").hide();
rows.find("td.SBS5 inputtvalue=2]").click(function() {
var nextRow = jQuery(this).closest("tr").next("tr");
if(nextRow.length==0) alert("That's a lot of sports/physical activity classes outside of school! Please use the text box on the next page to record any additional sports/physical activity classes your child participated in over the past year.");
else nextRow.show();
});
});

 

Thank you @TomG! It said there was an invalid token to start with, but I deleted the last }); and it has worked. Thanks so much!!


Leave a Reply