Set background image to a div in LWC

1.4K    Asked by ConnorPeake in Web-development , Asked on Aug 1, 2021
I am findiing Divs dynamically in my LWC component using data-id and that works. But when i am trying to set background image, it says.cannot read of property style of undefined. here is my code:
wiredResult({ error, data }) { if (data) { this.result=data; for(var i=0;i

any hrlp wil be appreciated. How javascript set background image?


Answered by Neelam Katherine

This may answer your question, you can test it in this playground playground

@sfdcfox did you share all code? otherwise, how do we get the event?

contacts; renderedCallback() { this.setBackgroundImage(); } setBackgroundImage() { const divs = this.template.querySelectorAll('div'); if (divs) { => { const div = Object.values(divs).filter(div => (div.getAttribute('data-id') === contact.Id))[0]; = `url(${contact.Offer_Image__c})`; }); } }

Your Answer


Parent Categories