Want to select a random element using jQuery? Use the following jQuery code to select a random element using jQuery.
In this example, We have used Math. random() function to get the random element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | (function($){ var random = 0; $.expr[':'].random = function(a, i, m, r) { if (i == 0) { random = Math.floor(Math.random() * r.length); } return i == random; }; })(jQuery); // This is how you use it: $('li:random').addClass('glow'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.