Listen for changes on selection of multiple choices and if a specific option is selected | XM Community
Skip to main content

1. I have a multiple choice question (multiple selections allows). I want to perform a function whenever any one of the options is selected or unselected. Currently, I'm using javascript as below but it doesn't work (i think):
jQuery("#" + "QID1" + " .Selection ").on("click change", function(){
// some functio
});
2. I'd also like to check if a specific option is selected or unselected. I'm using javascript below but it seems also not work:
if(jQuery("#QR~QID1~1").prop("checked") == true)
{
// some function
};
if(jQuery("#QR~QID1~1").prop("checked") == false)
{
// some function
};
Eventually, I want to achieve some code that checks dynamically:

  • if any option is just selected or unselected (or if any option is just 'clicked');

  • if selected, which one;

  • if unselected, which one.

and performs different actions when each option is selected and unselected like:
jQuery("#" + "QID1" + " .Selection ").on("click change", function(){
if(jQuery("#QR~QID1~1").prop("checked") == true)
{
// a
};
if(jQuery("#QR~QID1~".prop("checked") == false)
{
// b
};
if(jQuery("#QR~QID1~2").prop("checked") == true)
{
// c
};
if(jQuery("#QR~QID1~2").prop("checked") == false)
{
// d
};
});

Does anyone have any suggestions?

I think this is something related:https://www.qualtrics.com/community/discussion/5521/using-javascript-jquery-to-listen-for-checkboxes-being-checked-or-not, especially the "q-checked" thing. But I'm not sure how to incorporate it into my scenario specifically (really newbie to javascript).

I think I found a solution:
var objCheck1 = jQuery( "input[id='QR\\~QID1\\~1']" ); 
var objCheckLabel1 = jQuery( "labellfor='QR\\~QID1\\~1']"); 
var objCheck2 = jQuery( "inputuid='QR\\~QID1\\~2']" );  
var objCheckLabel2 = jQuery( "labelafor='QR\\~QID1\\~2']");  

var listen1 = function(e) {
if (typeof e.isTrigger == 'undefined') {    
var n = (objCheckLabel1.attr('class')).search("q-checked"); 
if (n > 0) {
// a
};
if (n == -1) {
// b
};
    };
};
var listen2 = function(e) {
    if (typeof e.isTrigger == 'undefined') {    
        var n = (objCheckLabel2.attr('class')).search("q-checked"); 
        if (n > 0) {
// c
};
if (n == -1) {
// d
};
   };
};

objCheck1.on('change',listen1);
objCheck2.on('change',listen2);


There's already a listener assinged for this. See the first example on this page.


Leave a Reply