I am trying to replace ul > li background color with image it is condition-based depending on #color. I have tried with css but no luck.
Help is appreciated
<ul class="tt-options-swatch options-middle filtres-js">
<li>
<a class="options-color" style="background:#000000" href="/collections/black">Black</a>
</li>
<li>
<a class="options-color" style="background:#FFFFFF" href="/collections/white">White</a>
</li>
<li>
<a class="options-color" style="background:#ccccff" href="/collections/violet">Violet </a>
</li>
</ul>
So what I am trying to do is IF style="background:#ccccff" then replace with image.
Thanks in advance.
Regards
3
Answers
I’ve made a clumsy example of how to do it here:
https://codepen.io/javiercampos/pen/abwwrWX
The code:
Note that I’m checking for the background-color CSS property that returns a string in the form
rgb(x, y, z)
, not necessarily how it’s applied in the HTML.I’m also removing the background color in the
a
(in all direct descendants of theli
basically) because that’s probably what you want.Note that this solution is pretty clumsy and will only work if the HTML is exactly how you paint it in the question, but it might be a start (also, the chosen images do not represent "white" or "black" at all, they are the first two results from google images searching for "color image")
See if it helps you
note: You set bgcolor on anchor tag. You can use window.getComputedStyle(list).backgroundColor for each element Like this
but window.getComputedStyle(list).backgroundColor return rgb, note a hexa color like `#fff
Couldn’t you just put the style inside the data attribute?
<a class="options-color" style="background:#FFFFFF" data-style="white" href="#">White</a>
.This way you could get the value of the data and use as
href
.const hrefColor = element.dataset.style
then
element.href = '/collections/'+hrefColor