Javascript help please?? | XM Community
Skip to main content
Question

Javascript help please??

  • November 14, 2024
  • 5 replies
  • 100 views

Forum|alt.badge.img+3
  • Level 2 ●●
  • 21 replies

Previously, this was put together for me (thanks so much): 

 

It had been working a charm ...but suddenly, the next lines no longer appear when ‘no’ is clicked. This survey is live, so we really need it working. Can someone please check my javascript and see what’s wrong?

 

Previously, we had issues with this not working in some browsers but it was fixed. I’ve just checked it in Firefox and it seems ok but in Chrome and Edge, it is not. Please help!

5 replies

mrlorey80
Forum|alt.badge.img
  • 6 replies
  • November 14, 2024

Question: Have you change the layout?
I’m experiencing some Custom Javascript issues with the Simple Layout.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 21 replies
  • November 14, 2024

No, no changes to any layout. One of the surveys I use it in hasn’t been edited for over a year. The question works in Firefox but not in Chrome or Edge. Previously it worked in all of them.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • November 14, 2024

@KateD,

There has been a change that has impacted the questionclick function sometime in the last couple of days.  I noticed it on an multiple choice question. When you click on a choice, the associated element is no longer the radio button or checkbox, it is the choice label or span within the label.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 21 replies
  • November 14, 2024
TomG wrote:

@KateD,

There has been a change that has impacted the questionclick function sometime in the last couple of days.  I noticed it on an multiple choice question. When you click on a choice, the associated element is no longer the radio button or checkbox, it is the choice label or span within the label.

 

Thanks for this. I figured there must have been a change somewhere. Below is our javascript - this was put together for me (I have no training in coding - I’m a nutrition researcher!). Is there any way to tweak this coding to force this question to work again please? I guess, an alternative would be to just remove the javascript altogether and leave the whole question displayed, which is not as neat but should still serve the purpose. Thanks so much!

 

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_rows[i].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 complex journey! Please use the text box on the next page to record any additional segments you made and/or comment on this particular journey.");
            } 
        }
    };/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/

});

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

});

 

 

And the question: 

 


vgayraud
QPN Level 5 ●●●●●
Forum|alt.badge.img+48
  • QPN Level 5 ●●●●●
  • 369 replies
  • November 15, 2024

Hello,

Try this :

Qualtrics.SurveyEngine.addOnload(function() {
    let quest = this;
    let qc = quest.questionContainer;
    let all_rows = qc.querySelectorAll(".Choice");

    for (let i = 1; i < all_rows.length; i++) {
        all_rows[i].style.display = 'none';
    }

    let radios = qc.querySelectorAll('input[type="radio"]');
    let noChoiceElement = radios[1];
    let noChoiceClass = noChoiceElement.parentElement.className.match(/c\d+/)[0];
    let no_id = Number(
        qc.querySelector("." + noChoiceClass + ":not(th)")
           .querySelector("input").id.split("~")[3]
    );

    function radioClick(event) {
        let choiceIdParts = event.target.id.split("~");
        let row = Number(choiceIdParts[2]);
        let choiceId = Number(choiceIdParts[3]);

        if (choiceId === no_id && row < all_rows.length) {
            all_rows[row].style.display = ''; 
        } else if (choiceId === no_id && row === all_rows.length) {
            alert("That's a complex journey! Please use the text box on the next page to record any additional segments you made and/or comment on this particular journey.");
        }
    }

    radios.forEach(function(radio) {
        radio.addEventListener('click', radioClick);
    });
});

 


Leave a Reply