Javascript not quite working as intended. Help needed | XM Community
Skip to main content
Hi Everyone,



I am using some javascript that allows for radio buttons to be de-selected. The issue I am having is that Qualtrics is still saving the original response even after it is deselected. Does anyone know how to fix this?



`Qualtrics.SurveyEngine.addOnload(function()

{

//initialize values

var prevValue = '';

var exampleForm = document.forms['Page'];

var labels = document.getElementsByTagName('LABEL');



//question click is a simple onclick handler

//attached to the question's container div

this.questionclick = function(event,element)

{

//by default you get the click event as the first parameter and the clicked element as the second parameter

if (element.type == 'radio')

{

//check if the radio was pressed in a row

if (element.id == prevValue)

{

for (var i = 0; i < labels.length; i++) {

if (labels[i].htmlFor == element.id) {

labels[i].className = labels[i].className.replace( /(?:^|\\s)q-checked(?!\\S)/g , '' );

labels[i].className = labels[i].className.replace( /(?:^|\\s)q-focused(?!\\S)/g , '' );

}

}

for( var i=0; i<exampleForm.length; i++ ){

//once found, uncheck it

if(exampleForm[i].id == element.id)

{

exampleForm[i].checked=false;

}

}

//clear temp value

prevValue = '';

}

else

{

//make last pressed button this one

prevValue = element.id;



}

}

}



});`
Have you tried setting an embedded data value based on selected choice?



I'd test if piped text still makes the answer show even when a selection is de-selected. If not, easy fix. If it still does, then you may need to adjust your JavaScript to set the embedded data based on what was selected.



Code:

Qualtrics.SurveyEngine.addOnReady(function(){

Qualtrics.SurveyEngine.addEmbeddedData ( 'embedded data name' , 'value' )



You'll probably need to figure a out a way to dynamically set 'value'

Leave a Reply