i'm a novice to jquery and i'm trying to do the following:
i've got multiple instances of a div. to each instance i randomly add a class (for changing some attributes). the added classes are taken from a list.
this works well — but now i'm trying to additionally add the same (random) class to a child-div of the previous.
my html is:
<div class="random">
<div class="alsorandom"> </div>
</div>
<div class="random">
<div class="alsorandom"> </div>
</div>
here's my current jquery (the randomization takes place so that 2 classes are never added after one another):
var classes = ['blue', 'yellow', 'lightorange', 'violet', 'green'];
var prevClass = "";
$('.randomcolor').each(function() {
var classes2 = [];
for (var i = 0; i < classes.length; i++) {
if (classes[i] !== prevClass) {
classes2.push(classes[i]);
}
}
$(this).addClass(prevClass = classes2[Math.floor(Math.random()*classes2.length)]);
});
the following i have tried and it doesn't work:
$('.randomcolor, .alsorandom').each(function() { ...
i'd be gracious for any help. thank you.
.randomcolorelement? without that class this will not work.