I've got some variables that I need to pass in jQuery/HTML for further use. I've got randomly generated number that I need to use later so I'm saving it as data-spin
on parent element.
When I'm trying to access the data, it stays the same - it's set once and even when the value is changed, the variable still says the first value.
$('#parent').scroll(function() {
var number = 1 + Math.floor(Math.random() * (12));
$('#parent').attr('data-spin', number);
console.log($('#parent').data('spin'));
});
I tried attaching the function to .scroll
function or no function at all but the result is same.
I'm attaching the jsfiddle example. The random number is logged at the first line, stored data at second.
Any ideas why it's not taking the current value of the field?
The variable is used in different function so I can't use the number
variable.