0

I am adding some list elements programmatically into a list, where every item has an image instead of a bullet. For that, I need to change also the image programmatically.

How can I set the url image path of the property 'background-image' from the li:before?

The css list properties look as:

ul.fileList {
    background-color: white;
    list-style: none;
}
    ul.fileList > li:before {
        content: '';
        display: inline-block;
        background-size: 30px;
        background-image: url('/Resources/imageFileIcon.png');
        background-repeat: no-repeat;
    }

I am adding the list items as:

   for (var i = 0; i < files.length; i++) {
            $("ul.fileList").append("<li><a href='#'>" + files[i] + "</a></li>");
            //how to set the background-image: url()??
   }
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • You can edit style rules using JS, or you can create a bunch of classes with different :before icons and set the class of the
  • instead.
  • –  Jan 14 '21 at 16:40
  • Add styles in
  • tag which you are appending
  • – Md Somir Jan 14 '21 at 16:42
  • Maybe this is what you're looking for? https://stackoverflow.com/a/7244951/12684693 – FSDford Jan 14 '21 at 16:43
  • Does this answer your question? [Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)](https://stackoverflow.com/questions/5041494/selecting-and-manipulating-css-pseudo-elements-such-as-before-and-after-usin) – Xth Jan 14 '21 at 16:43