I am trying to add classes to three divs with the same class, but I don't want any of the classes being added to repeat.
I have a script to add a class to 1, 2 or 3 divs that display at the same time. The desired effect is to have images displaying as background images, and in my stylesheet I have multiple classes with these images attached to them. These classes are defined and added to the relevant divs in the following code:
Javascript
$(document).ready(function(){
var classes = ["a01", "a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11"];
$(".S").each(function(){
$(this).addClass(classes[~~(Math.random()*classes.length)]);
});
});
This code is attached to the following HTML
<div class="brick">
<div class="blend S"></div>
<div class="blend S"></div>
<div class="blend S"></div>
</div>
Here is an example of one result this combination might currently return:
<div class="brick">
<div class="blend S a01"></div>
<div class="blend S a01"></div>
<div class="blend S a06"></div>
</div>
Note the repetition of a01. The following is the desired result:
<div class="brick">
<div class="blend S a05"></div>
<div class="blend S a02"></div>
<div class="blend S a11"></div>
</div>
Here is a live test. If anybody has any jquery advice for how I can circumvent the repetition issue, I would be super grateful. Thank you!