I am just working on a new website and would like javascript to assign a colour from ~10 different possible, pre-selected colours to a CSS div. The page is an index page of projects, and I'd like every project title (div.title) to take a different colour from an array. I am pretty new to javascript. Is this possible? If so, would it also be possible for it to select always the next colour in line in a loop (red, green, blue, yellow, for example, then repeat from the top)?
EDIT 01: I am customising a cargocollective website, so there are lots of limitations in terms of what can be done. I thought this way would be possible because I used the following code to change a:hover states:
$(document).ready(function()
{
$("a").hover(function(e)
{
var randomClass = getRandomClass();
$(e.target).attr("class", randomClass);
});
});
function getRandomClass()
{
//Store available css classes
var classes = new Array("red", "blue", "yellow", "navy", "orange", "green", "aqua",);
//Give a random number from 0 to 5
var randomNumber = Math.floor(Math.random()*7);
return classes[randomNumber];
}
and the applied this to the CSS:
a.red:hover { color: #f15123; }
a.blue:hover { color: #3491dd; }
a.yellow:hover { color: #f4b138; }
a.navy:hover { color: #3d57a7; }
a.orange:hover { color: #fd7b23; }
a.green:hover { color: #6fb766; }
a.aqua:hover { color: #4dd4af; }
I am trying to target div.title and would like the text colour to be different in every instance it shows up. Does that make more sense? If there's an easier way to do it, that would be great.
EDIT 02:
I am guessing my problem is the way the page is structured. I can't directly edit the HTML, only add scripts and some custom HTML. Here's what it looks like:
<div thumbnails="grid" grid-row="" thumbnails-pad="" thumbnails-gutter="" data-elementresizer="">
<div class="thumbnail has_title" data-id="6103627" grid-col="x10" thumbnails-pad="">
<a href="/Foxes-on-the-Hill" rel="history" class="image-link">
<div class="thumb_image scroll-transition-fade" style="height: 0px; padding-bottom: 100%; transition-duration: initial;" data-elementresizer-no-resize="" data-elementresizer-no-centering="">
<img data-mid="30797153" width="1200" height="1200" style="width: 100%; height: auto;" src="//freight.cargocollective.com/w/750/i/061c5d22a0ab1b7513812587c2ccf0f078542537cb129f9cd92159f26c925f80/1.jpg">
</div>
<div class="title"><span>Foxes on the Hill</span></div>
</a>
<div class="tags"><a href="/Cartography" rel="history">Cartography</a> </div>
</div>
<div class="thumbnail has_title" data-id="6103628" grid-col="x10" thumbnails-pad="">
<a href="/The-Curtains-in-the-House-of-the-Metaphysician" rel="history" class="image-link">
<div class="thumb_image scroll-transition-fade" style="height: 0px; padding-bottom: 100%; transition-duration: initial;" data-elementresizer-no-resize="" data-elementresizer-no-centering="">
<img data-mid="30797166" width="1200" height="1200" style="width: 100%; height: auto;" src="//freight.cargocollective.com/w/750/i/8be578d25086d836423959cda5323088cf6305bc0de35d102105a0ea46a75235/2.jpg">
</div>
<div class="title"><span>The Curtains in the House of the Metaphysician</span></div>
</a>
<div class="tags"><a href="/Illustration" rel="history">Illustration</a></div>
</div>
<div class="thumbnail has_title" data-id="6103629" grid-col="x10" thumbnails-pad="">
<a href="/The-Common-Life" rel="history" class="image-link">
<div class="thumb_image scroll-transition-fade" style="height: 0px; padding-bottom: 100%; transition-duration: initial; will-change: opacity, transform;" data-elementresizer-no-resize="" data-elementresizer-no-centering="">
<img data-mid="30797176" width="1200" height="1200" style="width: 100%; height: auto;" src="//freight.cargocollective.com/w/750/i/77a8b7c4d5a46922dea1c1484793a1d7b204f60a4bbf4d9d22d2276d4c085cc2/3.jpg">
</div>
<div class="title"><span>The Common Life</span></div>
</a>
<div class="tags"><a href="/Dimensionality" rel="history">Dimensionality</a></div>
</div>
</div>
And the CSS that goes with it:
/**
* Thumbnails
*/
div[thumbnails] {
justify-content: flex-start;
}
[data-css-preset] .thumbnails {
background-color: rgba(0, 0, 0, 0)/*!thumbnails_bgcolor*/;
}
[data-css-preset] .thumbnails_width {
width: 100%/*!thumbnails_width*/;
}
[data-css-preset] [thumbnails-pad] {
padding: 1.5rem/*!thumbnails_padding*/;
}
[data-css-preset] [thumbnails-gutter] {
margin: -3rem/*!thumbnails_padding*/;
}
[data-css-preset] [responsive-layout] [thumbnails-pad] {
padding: 0.5rem/*!responsive_thumbnails_padding*/;
}
[data-css-preset] [responsive-layout] [thumbnails-gutter] {
margin: -1rem/*!responsive_thumbnails_padding*/;
}
.thumbnails .thumb_image {
outline: 0px solid rgba(0,0,0,.12);
outline-offset: -1px;
}
.thumbnails .title {
margin-top: 1.2rem;
margin-bottom: 0.2rem;
font-size: 1.7rem;
font-weight: 400;
color: rgba(0, 0, 0, 0.85);
font-family: Interstate, Icons /*!Persona*/;
font-style: normal;
line-height: 1.3;
text-align: left;
}
.thumbnails .tags {
margin-top: 1.2rem;
margin-bottom: 0.5rem;
font-size: 1.7rem;
font-weight: 400;
color: rgba(0, 0, 0, 0.35);
font-family: Interstate, Icons /*!Persona*/;
font-style: normal;
line-height: 1.3;
text-align: left;
}
.thumbnails .tags a {
border-bottom: 0;
color: rgba(0, 0, 0, 0.25);
text-decoration: none;
}
.thumbnails .has_title .tags {
margin-top: 0rem;
}
It is the .title that I am trying to target, but nothing really seems to work. Thanks again!