I'm importing images:
import vanilla from 'url:../../img/flavors/vanilla.jpg';
import chocolate from 'url:../../img/flavors/chocolate.jpg';
I have several p tags that list several flavors:
<p class="cake-flav cake-flav--classic">vanilla</p>
<p class="cake-flav cake-flav--classic">funfetti</p>
<p class="cake-flav cake-flav--classic">chocolate</p>
I added a mouseover eventlistener to the container that houses the p tags above:
const flavorsBox = document.createElement('div');
flavorsBox.classList.add('main-content__container--flavors__flavors-box');
flavorsBox.addEventListener('mouseover', function(e){
const hovered= e.target.closest('.cake-flav');
if(hovered === null) return;
flavorImg.style.backgroundImage= '';
flavorImg.style.backgroundImage= `url(${hovered.textContent})`;
console.log(flavorImg.style.backgroundImage);
});
I'm currently taking the text content of the p tag that's being hovered and setting the background Image url to the text content in the hopes that it would refer to the named import of the images. What it is doing instead is `url("vanilla") and not referring to vanilla, the named import.