1

Is there a jQuery function that could randomize multiple dropdown select menus on a page, if they all have the same class - even if they are different lengths? So when a person loads a page, different options are selected by default?

I found this article Need a jQuery randomly selected identifier from options available, but it only appears to work with one select.

example:

<select class="selector">
 <option>Option A</option>
 <option>Option B</option>
 <option>Option C</option>
 <option>Option D</option>
</select>

<select class="selector">
 <option>Option A</option>
 <option>Option B</option>
 <option>Option C</option>
</select>

<select class="selector">
 <option>Option A</option>
 <option>Option B</option>
 <option>Option C</option>
 <option>Option D</option>
 <option>Option E</option>
</select>
Community
  • 1
  • 1
Jeremy
  • 925
  • 2
  • 11
  • 22

1 Answers1

4

You want something like this: http://jsfiddle.net/LV8ty/10/ It's basically the example from the post you linked to, just tweaked a bit, where I've added the loop for each select

$(".selector").each(function () {
var options = $(this).children('option');
var random = Math.floor(options.length * (Math.random() % 1));
options.attr('selected', false).eq(random).attr('selected', true);
});
mewm
  • 1,227
  • 10
  • 13