I want to create a data element that saves the distance a customer scrolls within a popup. I can return the value I'm after in the browser console using the following:
// Full height of div including scroll
var totalHeight = $("#myDialogContent").scrollHeight;
// Initial height of div that needs to be subtracted
var initialHeight = $("#myDialogContent").clientHeight;
// Actual element height value
var height = totalHeight - initialHeight;
// Scroll depth
var scroll = $("#myDialogContent").scrollTop();
var scrollPercentage = (scroll/height) * 100;
var rounded = Math.round(scrollPercentage);
This is the code I've used in the data element. However, when I try to return the value of the data element in the browser I get undefined each time.
This worked when I added it into a rule. I don't know why it didn't return through the console but I created a dummy data element change rule referencing the data element created using the above code and it worked as required.