"elems.sort is not a function" in jQuery 1.3.1

I needed to randomize the answer options for the little game that I create using html / jquery.

I came across a jQuery Randomize plugin that was published by Russ Cam on Qaru in October 2009. (see here. Randomize the sequence of div elements with jQuery ).

This plugin works great as a solution ... EXCEPT ...

For some reason, it does not 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 in my test file instead of 1.3.1, it works like magic.

But there rub. The site on which I create the game is blocked in jQuery 1.3.1. It is impossible to change.

So, two things:

  • Can someone help me figure out what is wrong or why is it not working? What happens in jQuery 1.3.1? I am new to js / jquery.

  • Can anyone suggest a workaround? An alternative way to write this line, which may work in version 1.3.1?

+3
source share
1 answer

Starting with jQuery 1.3.2 , jQuery proxied a built-in function Array.prototype.sortto work with jQuery object.

With jQuery 1.3.1, you cannot directly use sortjQuery for the object, but it is easy enough to convert the jQuery object to your own array to apply sorting:

var elems = $this.children(childElem).get(); // notice the .get()
elems.sort(function() { return (Math.round(Math.random())-0.5); });
+4

All Articles