Skip to content

Pass url value from background image style property

I have html code below:

<li class="name1 name2 name3" data-animate-effect="fadeIn" style="background-image: url(images/gallery-1.jpg); ">

Then how to set images/gallery-1.jpg to my imageURL variable javascript by querySelector? This is my try and error:

let imageURL = gallery[newIndex].querySelector("li").style.background.url;

Answer

A little more code needed

You can change document.querySelector("[data-animate-effect]") to gallery[newIndex].querySelector("li") if preferred

console.log(document.querySelector("[data-animate-effect]")
  .style.backgroundImage.match(/"(.*)"/)[1])
<li class="name1 name2 name3" data-animate-effect="fadeIn" style="background-image: url(images/gallery-1.jpg); ">