Working with lwc code, sometimes we got the requirement to get the index from the loop element. So, Here is the way how we can get that.

HTML:-

<template for:each={records} for:item="rec" for:index="index">
            <div class="slds-grid slds-wrap borderTopBottom" data-index={index} data-id={rec.id}  onclick={getData} ></div>
</template>

JS:-

getData(event){

    console.log('index :: ',event.currentTarget.dataset.index);
    console.log('record id :: ',event.currentTarget.dataset.id);
}

Hit the like button, If your problem gets solved. You can comment below , If you have any query.

THANKYOU | HAPPY LEARNING

Leave a Reply

Your email address will not be published. Required fields are marked *