I have a total of six div elements on a single page, all sharing the same class. My goal is to give each one a unique color from an array I have prepared. I want to avoid any repetition of colors among these divs.
Currently, I have managed to assign background colors to each div from the array randomly. However, there is a chance that some divs might end up with the same color due to the random selection process.
jQuery(document).ready(function($) {
$(".et_pb_post").each(function() {
var colors = ["#CFEAEA","#c9e3d5","#e7dadd","#dde9eb","#ecfac7","#facba9","#dfdbd3","#f1fdc1"];
var rand = Math.floor(Math.random()*colors.length);
$(this).css("background-color", colors[rand]);
});
});
The corresponding HTML code looks something like this:
<div class="et_pb_post">Some content with bg-color A</div>
<div class="et_pb_post">Some content with bg-color B</div>
<div class="et_pb_post">Some content with bg-color C</div>
<div class="et_pb_post">Some content with bg-color D</div>
Is there a way to ensure that each background color used is truly unique?
Thank you for your assistance!