Totalling hours and minutes from a matrix question | XM Community
Solved

Totalling hours and minutes from a matrix question

  • 22 March 2021
  • 1 reply
  • 302 views

I am completely new to Qualtrics and I have no experience with HTML or JavaScript, so I am hoping someone in the Qualtrics Community can help me.
I have set up a matrix question that asks survey respondents how much time their business spent on seven different tax compliance activities, by asking them to input numbers into side-by-side text boxes - see image below. I need to calculate the total amount of time spent on all activities, and then pipe that total (expressed in hours and minutes) into the subsequent question. The total box options in the matrix question don't work here, as they do a simple total of the numbers in each text box. I need the overall total to be expressed in hours and minutes, eg. 2 hours and 43 minutes, and NOT 1 hour and 103 minutes. Any assistance would be much appreciated - cheers!
GST compliance question.JPG

icon

Best answer by ahmedA 22 March 2021, 10:18

View original

1 reply

Userlevel 7
Badge +21

Try this JS:
Qualtrics.SurveyEngine.addOnReady(function () {
    let quest = this;
    let all_inputs = Array.from(quest.questionContainer.querySelectorAll("input[id^=QR\\\\~]"));
    let hrs = [],
        mns = [];
    all_inputs.forEach((item) => {
        if (item.id.split("~")[3] == 1) {
            hrs.push(item);
        } else if (item.id.split("~")[3] == 2) {
            mns.push(item);
        }
    });
    document.querySelector("#NextButton").onclick = function () {
        let tot_time = 0;
        hrs.forEach((item, index) => {
            tot_time += 60 * Number(item.value);
            tot_time += Number(mns[index].value);
        });
        let mins = tot_time % 60;
        let hours = (tot_time - mins) / 60;
        let time_spent = hours + " Hours and " + mins + " Minutes";
        Qualtrics.SurveyEngine.setEmbeddedData("time_spent", time_spent);
    };
});
You'll get an embedded variable called

time_spent
, which you can pipe to other questions.

Leave a Reply