Search code examples
javascriptqualtrics

How do I create a javascript variable in qualtrics that represents if a checkbox is ticked or not?


I have this code for a popup button confirming that the respondent understands the survey will be terminated if they don't agree to the consent form:

Qualtrics.SurveyEngine.addOnReady(function()
{
 /*Place your JavaScript here to run when the page is fully displayed*/
const that=this;
    this.hideNextButton();
    var btn = jQuery('<div id="Buttons"><input id="CustomButton" class="NextButton Button"  title="  Next  " type="button" name="NextButton" value=" Next  "  aria-label="Next"></div>')
    jQuery('#Buttons').append(btn);

    jQuery('#CustomButton').on('click', function(){
        var txt;
        var e= jQuery("[id='QID107']").val()
        if(e==""){
                    var r = confirm("The survey will be terminated if you do not agree with the consent form. Are you sure you wish to continue?");
                     if (r == true) {
                            that.clickNextButton();
                     } else {

                    }
        }
    });
});

I'm a novice in javascript and would like to have the variable e to refer to the choice of question QID107 which is a single checkbox for the consent form. I only want the popup with the message "The survey will be terminated (...). Are you sure you wish to continue?" to be displayed if the checkbox is left unticked and e is empty. That is what I'm trying to do with if e=="". It looks like that condition is true regardless of the answer to the consent question, so I think I'm doing something wrong but don't know what. How do I create that condition in this script? How do I correctly get the response to the consent form into e?

EDIT: The question has a condition that terminates the survey if the checkbox is left empty, so that.clickNextButton() when the checkbox is empty activates that termination condition.


Solution

  • Instead of checking equality for 'e' with empty string you have to check it's checked state.

    jQuery("[id='QID107']").is(":checked")
    

    This return the state of checkbox, with this you get to know that checkbox is checked or not