Skip to main content
deleted 153 characters in body
Source Link
Caspar Harmer
  • 20.6k
  • 4
  • 36
  • 69

I have been trying to set background-image to divs inside Template iteration in LWC component. But when I am trying to locate divs using this.template.queryLocatorAll, I am not getting any Divs. Is it because they are inside Template iteration. If so then how can iI locate these elements and set background images to these divs?

Here is my HTML code:

 <div class="slideshow-container">
            <template for:each=each="{result}" for:item="res">
            <div class="mySlides fade"  key=key="{res.id} >">
                <div data-id= id="{res.Id} " class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
      

           <div>
  <div >
                 {res.Id}
                {res.Comments__c}
            </div>
        </div>
         
    </div>
    
     </div>
    </template>
</div>

and here is my JS code:

 const divs = this.template.querySelectorAll('div');
for (var i = for(var0; i=0;i<thisi < this.result.length;i++length; i++) {
        const  target = this.template.querySelectorAll('[data-id='+thisid=' + this.result[i].id+']'id + ']');
       console.log('target'+target'target' + target.length);
    }
    if (divs) {
        console.log('divs'+divs'divs' + divs.length);
  for (var i = 0; i for(var< i=0;i<divsdivs.length;i++length; i++) {
        this.result.map((res) => {

            const div = Object.values(divs).filter(res => (res.getAttribute('data-id') === res.Id))[i];
            div.style.backgroundImage = 'url()';
        });
    }
 
    } 

Any help will be appericatedappreciated.

I have been trying to set background-image to divs inside Template iteration in LWC component. But when I am trying to locate divs using this.template.queryLocatorAll, I am not getting any Divs. Is it because they are inside Template iteration. If so then how can i locate these elements and set background images to these divs?

Here is my HTML code:

 <div class="slideshow-container">
            <template for:each={result} for:item="res">
            <div class="mySlides fade"  key={res.id} >
                <div data-id= {res.Id}  class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
      

             <div >
                {res.Id}
                {res.Comments__c}
            </div>
        
         
        
     </div>
    </div>

and here is my JS code:

 const divs = this.template.querySelectorAll('div');
    for(var i=0;i<this.result.length;i++){
        const  target = this.template.querySelectorAll('[data-id='+this.result[i].id+']');
       console.log('target'+target.length);
    }
    if (divs) {
        console.log('divs'+divs.length);
        for(var i=0;i<divs.length;i++){
        this.result.map((res) => {

            const div = Object.values(divs).filter(res => (res.getAttribute('data-id') === res.Id))[i];
            div.style.backgroundImage = 'url()';
        });
    }
 
    } 

Any help will be appericated.

I have been trying to set background-image to divs inside Template iteration in LWC component. But when I am trying to locate divs using this.template.queryLocatorAll, I am not getting any Divs. Is it because they are inside Template iteration. If so then how can I locate these elements and set background images to these divs?

Here is my HTML code:

<div class="slideshow-container">
    <template for:each="{result}" for:item="res">
        <div class="mySlides fade" key="{res.id}">
            <div data-id="{res.Id}" class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
                <div>
                    {res.Id} {res.Comments__c}
                </div>
            </div>
        </div>
    </template>
</div>

and here is my JS code:

const divs = this.template.querySelectorAll('div');
for (var i = 0; i < this.result.length; i++) {
  const target = this.template.querySelectorAll('[data-id=' + this.result[i].id + ']');
  console.log('target' + target.length);
}
if (divs) {
  console.log('divs' + divs.length);
  for (var i = 0; i < divs.length; i++) {
    this.result.map((res) => {

      const div = Object.values(divs).filter(res => (res.getAttribute('data-id') === res.Id))[i];
      div.style.backgroundImage = 'url()';
    });
  }
}

Any help will be appreciated.

Source Link
Amit Rajput
  • 133
  • 2
  • 11

This.template.queryLocatorAll not working inside Template if

I have been trying to set background-image to divs inside Template iteration in LWC component. But when I am trying to locate divs using this.template.queryLocatorAll, I am not getting any Divs. Is it because they are inside Template iteration. If so then how can i locate these elements and set background images to these divs?

Here is my HTML code:

 <div class="slideshow-container">
            <template for:each={result} for:item="res">
            <div class="mySlides fade"  key={res.id} >
                <div data-id= {res.Id}  class="slds-box slds-box_x-small slds-text-align_center slds-m-around_x-small">
      

            <div >
                {res.Id}
                {res.Comments__c}
            </div>
        
         
       
    </div>
    </div>

and here is my JS code:

 const divs = this.template.querySelectorAll('div');
    for(var i=0;i<this.result.length;i++){
        const  target = this.template.querySelectorAll('[data-id='+this.result[i].id+']');
       console.log('target'+target.length);
    }
    if (divs) {
        console.log('divs'+divs.length);
        for(var i=0;i<divs.length;i++){
        this.result.map((res) => {

            const div = Object.values(divs).filter(res => (res.getAttribute('data-id') === res.Id))[i];
            div.style.backgroundImage = 'url()';
        });
    }

    } 

Any help will be appericated.