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 query dataset attribute in JS lwc

Hello Everyone,
I am using lightning input field on record edit form to creat a record and my requirement is to query the data attribute of lightning input field. Is there any way to query the specific data attribute which matches with the field of 'this.template.querySelectorAll('lightning-input-field').forEach((field) => {)};'
Please let me know on the same.Thanks in Advance
SubratSubrat (Salesforce Developers) 
Hello Shuhbam ,

You can query the data attribute of a Lightning input field using the dataset property. Here's an example of how you can modify your existing code to retrieve the data attribute value:
 
this.template.querySelectorAll('lightning-input-field').forEach((field) => {
    if (field.fieldName === 'MyFieldApiName') {
        const myDataAttributeValue = field.dataset.myDataAttribute;
        // Do something with the data attribute value
    }
});

If it helps please mark this as Best Answer.
Thank you.