instead of this.questionclick , count mouseup event | XM Community
Skip to main content
For a question in my survey I need to count the number of clicks that were done.



The Click Count of Qualtrics (using either javascript's this.questionclick or using the Timing item's Click Count variable) only works for certain types of questions. It does not work, for example, for Ranking questions in which the participant must drag and drop. The dragging and dropping nature of the mouse click prevents it from registering it as a click. I therefore need a code that uses javascript's mouseup event (i.e. everytime a participant releases the left mouse button after having clicked it).



So, something like this:



object.onmouseup = function(){

var n = Qualtrics.SurveyEngine.getEmbeddedData('n');

n = n + 1;

Qualtrics.SurveyEngine.setEmbeddedData('n', n);

};



Except the above code doesn't work. I don't actually know how to use javascript (but I do have a basic understanding of programming languages) so I know I'm missing something, I'm just not sure what exactly I'm missing. Like, do I need to add an EventListener for this to work?



Thanks for your help!
In your example it isn't clear what 'object' is. For it to work, it has to be an element on which the event is taking place.



You should probably use a drop event instead of a mouseup event so you only count valid drops. You would add the event to the element where you are dropping the element being dragged (the "drop zone").



Also, you can't use getEmbeddedData/setEmbeddedData that way. Initialize the counter at 0 (or pipe it if you need to support previous button capability) before the event handler and increment it inside the event handler JS. You can just set the final count as embedded data at the end (addOnPageSubmit).
My programming friend gave me the following code, which works.

In case someone else needs it ever:



var n = Qualtrics.SurveyEngine.getEmbeddedData('n');

var mouseUpTracker = function() {

n = n + 1;

Qualtrics.SurveyEngine.setEmbeddedData('n', n);

}



Qualtrics.SurveyEngine.addOnload(function()

{

/*Place your JavaScript here to run when the page loads*/



});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

document.addEventListener('mouseup', mouseUpTracker);

});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/

document.removeEventListener('mouseup', mouseUpTracker);

});

Leave a Reply