Back jQuery

Create a jQuery Random Filter

WRITTEN BY ON 31 Jul 2011
14,031 VIEWS • SHARES
2 comments

This simple and handy jQuery filter return a single random item from a selection of items. Credit to Waldek Mastykarz.

JS - jQuery

Usage:

$('item:random').text();

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

2 comments
du hoc 13 years ago
thank your post
Reply
Max Pen 10 years ago
Can I have a demo? How to use this?
Reply