Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
ul > li replace color with image (depends on #color condition

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I've made a clumsy example of how to do it here:

https://codepen.io/javiercampos/pen/abwwrWX

The code:

  $('ul>li').each(function() {
    if($(this).children().css("background-color") == 'rgb(255, 255, 255)') {
      $(this).css('background-image', 'url(//media.istockphoto.com/vectors/abstract-blurred-colorful-background-vector-id1185382671?k=20&m=1185382671&s=612x612&w=0&h=QvHSiV0uDYhl69m1rpIt0aYbk4vmpl9kjVcfkMkgyfw=)'); 
      $(this).children().css('background-color', "");
    }
    if($(this).children().css("background-color") == 'rgb(0, 0, 0)') {
      $(this).css('background-image', 'url(//miuc.org/wp-content/uploads/2018/02/How-can-colours-help-you-in-your-everyday-life.jpg)'); 
      $(this).children().css('background-color', "");
    }
  });

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 the li 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

about 4 years ago · Juan Pablo Isaza Report

0

note: You set bgcolor on anchor tag. You can use window.getComputedStyle(list).backgroundColor for each element Like this

// select all a tag
var lists = document.querySelectorAll('ul > li > a');

// looping foreach
lists.forEach(list => {
  const bgcol = window.getComputedStyle(list).backgroundColor;
  console.log(bgcol);
  bgcol == 'rgb(204, 204, 255)' ? console.log('Change to Img') : console.log('Skip');
})
<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>

but window.getComputedStyle(list).backgroundColor return rgb, note a hexa color like `#fff

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!