I needed to randomize answer choices for a little game I'm building using html/jquery.
I came across a jQuery Randomize plugin shared by Russ Cam on Stack Overflow on Oct. 2009 (see here: Randomize a sequence of div elements with jQuery).
This plugin works great as a solution.... EXCEPT...
For some reason it doesn't seem to work with jQuery 1.3.1. Firebug throws an error ("elems.sort is not a function") on this line:
elems.sort(function() { return (Math.round(Math.random())-0.5); });
Now, if I include jQuery 1.3.2 on my test file instead of 1.3.1, it works like magic.
But there's the rub. The site I'm building the game for is locked into jQuery 1.3.1. That cannot be changed.
So, two things:
Can anyone help me understand what is wrong, or why it's not working? What is it that is broken or missing in jQuery 1.3.1? I am new to js/jquery.
Can anyone suggest a workaround? An alternate way to write that line that might work in 1.3.1?
From jQuery 1.3.2 onwards, jQuery proxied the native
Array.prototype.sort
function to work with a jQuery object.With jQuery 1.3.1, you can't directly use
sort
on a jQuery object, but it is easy enough to convert a jQuery object to a native array to apply the sort on: