I have the following code:
<div>
<span>Random text</span>
<span style="color: red">Random text</span>
</div>
and, using code, I have a 50% chance of changing the order of the elements inside the div.
$(document).ready(function() {
var initial = $("div"); //This might not be ok
if (new Date() % 2) //50% chance to randomize order
randomizeOrder();
var after = $("div"); //Also this might not be ok
//Detect if the order of spans changed
if (after != initial) //Definitely won't work
console.log("Order changed");
})
function randomizeOrder() {
var lastElement = $("div span").last();
$(lastElement).insertBefore($("div span").first());
}
This example is oversimplified. The ordering is done by the user using a 3rd party library which won't tell me if the order actually changed, just tell me when it started & finished.
How can I detect if the order of the DOM elements actually changed? The information I have inside them cannot be used, as it can be exactly the same, as in the example.