Custom code js works in question preview but not survey | XM Community
Skip to main content

I believe similar questions have been asked quite a bit, but I have not seen a solution. I made a drag-and-drop question, which works just fine when I preview the question but does not work if I preview the block, preview the survey, or take the survey. The JS portion of the code is below, and I would appreciate it if anyone has any insight on how I could make it work! Thank you!

 

Qualtrics.SurveyEngine.addOnload(function () {
const draggableIcons = document.querySelectorAll('#breakfast, #dinner');

draggableIcons.forEach(icon => {
let isDragging = false;
let offsetX, offsetY;

// Start dragging
icon.addEventListener("mousedown", (event) => {
isDragging = true;
offsetX = event.clientX - icon.getBoundingClientRect().left;
offsetY = event.clientY - icon.getBoundingClientRect().top;
icon.style.cursor = "grabbing"; // Change cursor on drag
event.preventDefault();

document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", onMouseUp);
});

// Handle dragging
function onMouseMove(event) {
if (isDragging) {
icon.style.left = `${event.clientX - offsetX}px`;
icon.style.top = `${event.clientY - offsetY}px`;
}
}

// Stop dragging and reset cursor
function onMouseUp() {
isDragging = false;
icon.style.cursor = "grab"; // Reset cursor
document.removeEventListener("mousemove", onMouseMove);
document.removeEventListener("mouseup", onMouseUp);
}
});
});

 

Try this out -

 

Qualtrics.SurveyEngine.addOnload(function () {
const draggableIcons = document.querySelectorAll('#breakfast, #dinner');


draggableIcons.forEach(icon => {
let isDragging = false;
let offsetX, offsetY;

icon.style.position = 'absolute';
icon.style.zIndex = '1000';

// Start dragging
icon.addEventListener("mousedown", (event) => {
isDragging = true;
offsetX = event.clientX - icon.getBoundingClientRect().left;
offsetY = event.clientY - icon.getBoundingClientRect().top;
icon.style.cursor = "grabbing"; // Change cursor on drag
event.preventDefault();

document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", onMouseUp);
});

// Handle dragging
function onMouseMove(event) {
if (isDragging) {
icon.style.left = `${event.clientX - offsetX}px`;
icon.style.top = `${event.clientY - offsetY}px`;
}
}

// Stop dragging and reset cursor
function onMouseUp() {
isDragging = false;
icon.style.cursor = "grab"; // Reset cursor
document.removeEventListener("mousemove", onMouseMove);
document.removeEventListener("mouseup", onMouseUp);
}
});

});

 


@resh_kr thank you so much! Unfortunately, this still doesn’t work! I tried out some console.log statements, and when I am on ‘question preview’ I get the correct logs (e.g., “breakfast is being dragged to: (248.333px, 244.333px), Stop dragging breakfast at final position: (248.333px, 244.333px)”). So I can both drag, and see that the items are being loaded/the correct names and positions are being logged. When I am taking the survey or using ‘preview block’, the logs are suggesting that the items and the coordinates aren’t loading (e.g., “  is being dragged to: (, ), Stop dragging  at final position: (, )”). Using addOnload or addOnready didn’t make a difference and I cannot seem to figure out what is causing the issue here!


Leave a Reply