I have the following function:
$(".schedule").each(function () {
var cls = this.className.match(/schedule-\d+/);
if (cls.length === 0) return;
var els = $(".schedule." + cls);
if (els.not(this).length > 0) {
els.addClass("someClass");
}
});
It adds a class (.someClass
) to divs that have the same class, turning the div red. As seen in this fiddle, it works. I'd like to add this someClass
class randomly to max 2 divs with the same class.
Example (ps: The order of the div classes are generated randomly and they are not limited to just A,B,C and D. This order and class names in the fiddle are just an example):
We have A, B, C, A, C, D, A.
I want randomly 2 divs with the same class turn red so:
- A and (second) A is red OR
- A and (third) A is red OR
- (second) A and (third) A is red OR
- C and C is red
If this is very complicated I'll settle for the first found match too. So:
- A and (second) A is red
Hope someone can help, thanks!