Count Multiple Choice Responses when using Column Alignment | XM Community
Skip to main content

I found a solution in the community that counts the number of responses across several multiple choice questions on a page (see code below).
However when I change the alignment of the multiple choice question from the default vertical layout to column layout the solution no longer works.
Below is the code:
let all_checkboxes = document.querySelectorAll(".q-checkbox");
all_checkboxes.forEach((cb) => (cb.parentElement.onmousedown = selected_choices));

function selected_choices() {
    let num_chosen = document.querySelectorAll(".q-checkbox.q-checked").length;

    if (num_chosen >= 2 && !this.querySelector(".q-checked")) {
        this.click();
        alert(
            "Please select up to two choices only. Please remove a choice before proceeding."
        );
    }
}
I've tried adjusting the code. I've tried inspecting the question to try and work out what has changed in the question, but I can't work it out.
1) What do I need to update in the code to make it work when the multiple choice questions are in column layout?
2) How do I find this out in the future?
Thank you

1: What do I need to update in the code to make it work?


Qualtrics.SurveyEngine.addOnReady(function () {
  let all_checkboxes = document.querySelectorAll(".q-checkbox")
  this.questionclick = selected_choices


  function selected_choices(event, element) {
    let num_chosen = document.querySelectorAll(".q-checkbox.q-checked").length


    if (num_chosen > 2 && !element.querySelector(".q-checked")) {
      event.target.click()
      alert(
        "Please select up to two choices only. Please remove a choice before proceeding."
      )
    }
  }
})
The issue with the original code is that, in a column layout,
cb.parentElement.onmousedown
is not actually effective in locating where the button was clicked in order to check for if there are too many selections, so
selected_choices
never runs. I've adjusted it to check for selected_choices anytime anything in the question is clicked, using the
this.questionclick
hook, more information can be found here. Because the callback changes, selected_choices has to take in the event and element, and work based off that. I also changed the criteria to if num_chosen is greater than 2 rather than greater or equal to 2.

2: How do I find this out in the future?


The JavaScript Question API reference is a good starting point in general. For debugging this code, it can help to add test alerts or console logs to see what parts of the code run, and which don't. For example, here is some code that helped me debug the original code. The outputs from the console logs can be viewed in DevTools, keyboard shortcut CTRL-SHIFT-I (i not L) typically.
console.log('Code is running');
let all_checkboxes = document.querySelectorAll(".q-checkbox");
console.log('All checkboxes:');
console.log(all_checkboxes);
all_checkboxes.forEach((cb) => (cb.parentElement.onmousedown = selected_choices));

function selected_choices() {
console.log('Checking for selected choices');
    let num_chosen = document.querySelectorAll(".q-checkbox.q-checked").length;

    if (num_chosen >= 2 && !this.querySelector(".q-checked")) {
        this.click();
        alert(
            "Please select up to two choices only. Please remove a choice before proceeding."
        );
    }
}



https://community.qualtrics.com/XMcommunity/discussion/comment/52388#Comment_52388Thanks srbaumruk - I tried the new code, but I don't think it's working? I tested it a number of times and it works on some occasions, but not always. Not sure why?


MikeW If there's a particular way you can replicate it not working, let me know. Also, if it does not work, I would recommend opening the Console tab in Chrome DevTools (CTRL-SHIFT-i) and seeing if there is an error message that could provide additional clarity.


https://community.qualtrics.com/XMcommunity/discussion/comment/52397#Comment_52397Hi - here is an example survey:
https://dtoperations.eu.qualtrics.com/jfe/preview/previewId/838ffc41-cb95-409c-8b21-44b4fb35b6c2/SV_8kVIv8oJU3H77v0?Q_CHL=preview&Q_SurveyVersionID=current
Screenshot 2022-11-20 180228.pngSo, in the screenshot above, I clicked 5 items before it displayed the message.


Leave a Reply