Hi all,
I have a survey where I've implemented a simple counter to track the number of times a user clicks on any region of a Hot Spot element. I've set up a click event listener for the path elements of the hot spot SVG, and on click, I get the embedded data field
hs_ct, increment by one, then set
hs_ctwith the updated value.
Here's a simple example: https://betterup.co1.qualtrics.com/jfe/form/SV_cTIkLpl3p9HHZtk
And here's the JS code for the hot spot item:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery('path').on('click', function() {
console.log('you clicked a region');
var ct = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("hs_ct"));
console.log('counter before increment: ' + String(ct));
ct = ct + 1;
console.log('counter incremented by 1: ' + String(ct));
Qualtrics.SurveyEngine.setEmbeddedData("hs_ct", ct);
var newCt = Qualtrics.SurveyEngine.getEmbeddedData("hs_ct");
console.log('counter set and then re-loaded, should show updated value: ' + String(newCt));
});
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
My problem is that none of my Javascript code seems to execute, unless I refresh the page. After one refresh, everything works. (Subsequent refreshes also work fine.)
This behavior occurs both in Preview mode and when using a live, Published survey. It's not just a console issue either - without refreshing after the survey is loaded for the first time, the embedded data field
hs_ctin a completed survey response does not reflect hot spot clicks - its value is zero.
If you open the Javascript console in your browser, you'll see that clicking on one of the four squares results in a set of console log entries. First, a message appears ("you clicked a region"), and then the click count is displayed, before and after incrementing and re-setting the ED field. But assuming the issue is reproducible, you won't see any console output on first load. Only after you reload will you see the console output showing up.
I've tried moving the code to the
onLoadsection (although I think it needs to be
onReadybecause of when the SVG is constructed), that didn't make a difference. I also tested in FF and Chrome, on the off chance it was a browser issue. The issue is the same in both browsers.
I have thought about setting an auto-refresh on page load, but that feels pretty hacky. Any ideas on what's causing this issue?
Thanks,
Andrew