function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shuhbam SinhaShuhbam Sinha 

How to pass dynamic id to 'querySelectorAll' in lwc

Hello Everyone,
I need to pass dynamic data -id to this.template.querySelectorAll("lightning-input-field[data-id= 'accountId']"); 

for (let i = 0; i < this.accounts.length; i++) {
var accountId = this.accounts[i].Id;
 const temp =   this.template.querySelectorAll("lightning-input-field[data-id= 'accountId']");
               console.log('temp',temp[2]);
               temp[2].value = dateToday.toJSON().slice(0, 10);
}
// var accountId is not working here
accountId is not working here but if i pass hard coded id like 
const temp = this.template.querySelectorAll("lightning-input-field[data-id= '0069I000003OWmc']");
 
html 

    <lightning-input-field 
                                                        field-name="Year_Months_To__c"
                                                        variant= "label-hidden"
                                                        data-id={acc.Id} 
                                                        value={yearsMonthsTo}
                                                        onchange={handleDateTo}
                                                        required> 
     </lightning-input-field>

Please help me on the same. Thanks in advance
Best Answer chosen by Shuhbam Sinha
Arun Kumar 1141Arun Kumar 1141

Hi Shubham,

You can refer to below code.

for (let i = 0; i < this.accounts.length; i++) {
var accountId = this.accounts[i].Id;
var fieldString = `lightning-input-field[data-id='${accountId}']`; //use template literals
 const temp =   this.template.querySelectorAll(fieldString);
               console.log('temp',temp[2]);
               temp[2].value = dateToday.toJSON().slice(0, 10);
}

If you want to use accountId dynamically, use template literals.

Please mark it as best answer if it helps.

Thanks,

All Answers

HarshHarsh (Salesforce Developers) 
Hi Shuhbam,

Please refer to the following Stack Exchange link which has the same issue, This might help you achieve your requirement.
https://salesforce.stackexchange.com/questions/256007/how-do-you-define-an-element-with-an-id-attribute-using-lwc

Related:-

https://developer.salesforce.com/forums/?id=9062I000000UhWfQAK (http://​​​​​​​https://developer.salesforce.com/forums/?id=9062I000000UhWfQAK

​​​​​​​Please mark it as Best Answer if the above information was helpful.

Thanks.
 
Arun Kumar 1141Arun Kumar 1141

Hi Shubham,

You can refer to below code.

for (let i = 0; i < this.accounts.length; i++) {
var accountId = this.accounts[i].Id;
var fieldString = `lightning-input-field[data-id='${accountId}']`; //use template literals
 const temp =   this.template.querySelectorAll(fieldString);
               console.log('temp',temp[2]);
               temp[2].value = dateToday.toJSON().slice(0, 10);
}

If you want to use accountId dynamically, use template literals.

Please mark it as best answer if it helps.

Thanks,

This was selected as the best answer
Shuhbam SinhaShuhbam Sinha
Thanks Arun