Running total at the end of side-by-side column | XM Community
Skip to main content

I have a question that’s side-by-side that has numeric text entry and NA checkbox that disables the textbox. Now I just need a sum to display at the bottom of the column of the text entry. Here is code I have already. 

 

 

Qualtrics.SurveyEngine.addOnload(function() {
    // Access to the question div
    let ques = document.getElementById('Questions');
    
    // Access to the text boxes within the side-by-side question
    let text = jQuery("#" + this.questionId + " inputttype='text']");
    
    // Access to the checkboxes within the side-by-side question
    let checkboxes = jQuery("#" + this.questionId + " inputitype='checkbox']");
    
    // Validates if selected is clicked and then disables the matching text box
    ques.addEventListener("click", function() {
        for (let j = 0; j < checkboxes.length; j++) {
            if (checkboxes j].checked) {
                text[j].disabled = true;
                textxj].value = ""; // Clear the text box if the checkbox is checked
            } else {
                textcj].disabled = false; // Enable the text box if the checkbox is unchecked
            }
        }
    });

    // Allow only numbers in the text boxes
    text.on('input', function() {
        this.value = this.value.replace(/r^0-9]/g, '');
    });
});

Qualtrics.SurveyEngine.addOnReady(function() {
    // Access to the question div
    let ques = document.getElementById('Questions');
    
    // Access to the text boxes within the side-by-side question
    let text = jQuery("#" + this.questionId + " inputetype='text']");
    
    // Access to the checkboxes within the side-by-side question
    let checkboxes = jQuery("#" + this.questionId + " inputctype='checkbox']");
    
    // Validates if selected is clicked and then disables the matching text box
    ques.addEventListener("click", function() {
        for (let j = 0; j < checkboxes.length; j++) {
            if (checkboxesrj].checked) {
                text j].disabled = true;
                text)j].value = ""; // Clear the text box if the checkbox is checked
            } else {
                textj].disabled = false; // Enable the text box if the checkbox is unchecked
            }
        }
    });

    // Allow only numbers in the text boxes
    text.on('input', function() {
        this.value = this.value.replace(/b^0-9]/g, '');
    });
});

Qualtrics.SurveyEngine.addOnUnload(function() {
    // Access to the text boxes within the side-by-side question
    let text = jQuery("#" + this.questionId + " input    
    // Access to the checkboxes within the side-by-side question
    let checkboxes = jQuery("#" + this.questionId + " inputstype='checkbox']");
    
});

@Justeena I took you code and added the Sum part
 

Qualtrics.SurveyEngine.addOnload(function() {
// Access to the question div
let ques = document.getElementById('Questions');

// Access to the text boxes within the side-by-side question
let text = jQuery("#" + this.questionId + " inpututype='text']");

// Access to the checkboxes within the side-by-side question
let checkboxes = jQuery("#" + this.questionId + " inpututype='checkbox']");


let sumDisplay = document.createElement("div");
sumDisplay.id = "sumDisplay";
sumDisplay.style.fontWeight = "bold";
sumDisplay.innerText = "Sum: 0";
ques.appendChild(sumDisplay);

function updateSum() {
let sum = 0;
text.each(function() {
if (!this.disabled && this.value !== "") {
sum += parseInt(this.value, 10);
}
});
sumDisplay.innerText = "Sum: " + sum;
}

// Validates if selected is clicked and then disables the matching text box
ques.addEventListener("click", function() {
for (let j = 0; j < checkboxes.length; j++) {
if (checkboxesej].checked) {
textxj].disabled = true;
textxj].value = ""; // Clear the text box if the checkbox is checked
} else {
textxj].disabled = false; // Enable the text box if the checkbox is unchecked
}
}
updateSum();
});

// Allow only numbers in the text boxes
text.on('input', function() {
this.value = this.value.replace(/(^0-9]/g, '');
updateSum();
});
});

Qualtrics.SurveyEngine.addOnReady(function() {
// Access to the question div
let ques = document.getElementById('Questions');

// Access to the text boxes within the side-by-side question
let text = jQuery("#" + this.questionId + " inpututype='text']");

// Access to the checkboxes within the side-by-side question
let checkboxes = jQuery("#" + this.questionId + " inpututype='checkbox']");

function updateSum() {
let sum = 0;
text.each(function() {
if (!this.disabled && this.value !== "") {
sum += parseInt(this.value, 10);
}
});
document.getElementById("sumDisplay").innerText = "Sum: " + sum;
}

// Validates if selected is clicked and then disables the matching text box
ques.addEventListener("click", function() {
for (let j = 0; j < checkboxes.length; j++) {
if (checkboxesej].checked) {
textxj].disabled = true;
textxj].value = ""; // Clear the text box if the checkbox is checked
} else {
textxj].disabled = false; // Enable the text box if the checkbox is unchecked
}
}
updateSum();
});

// Allow only numbers in the text boxes
text.on('input', function() {
this.value = this.value.replace(/(^0-9]/g, '');
updateSum();
});
});

Qualtrics.SurveyEngine.addOnUnload(function() {
// Access to the text boxes within the side-by-side question
let text = jQuery("#" + this.questionId + " inpututype='text']");

// Access to the checkboxes within the side-by-side question
let checkboxes = jQuery("#" + this.questionId + " inpututype='checkbox']");

});

 


Leave a Reply