I am working in LWC requirements where I have a list of records using this list I need to create inputs text. Once I loop through the list in LWC 'HTML component' my input texts are coming as aspected but while I enter the value in the text box the same value is updated to the 2nd element of the array. Below is my code.

 <template for:each={selectedCons} for:item="oli" for:index="index"> 
     <template for:each={olilist} for:item="oli2" for:index="ids"> 
        <tr key={oli.index}>
               <lightning-input name='oliUnitPrice' access-key={index} type='Currency' id={index}  onchange={changeHandler} value={oli2.UnitPrice}>
           <lightning-input name='oliQuantity' access-key={index} type='Number' id={index}  onchange={changeHandler} value={oli2.Quantity}>

enter image description here

If I enter 7 the 2nd rows also gets updated with 7.

My Js Code below.

@track selectedCons; 
@track olilist = [
        Name: '',
        ProductCode: '',
        UnitPrice: '',
        Quantity: '',
        Product2Id : ''

showContacts() {
    this.bShowModal = true;

    this.selectedCons = [];

    let selectedRows = this.template.querySelectorAll('lightning-input'); 

    // based on selected row getting values of the contact
    for(let i = 0; i < selectedRows.length; i++) {
        if(selectedRows[i].checked && selectedRows[i].type === 'checkbox') {
                Name: selectedRows[i].value,
                Id: selectedRows[i].dataset.id,
                ProductCode : selectedRows[i].value,
                Product2Id : selectedRows[i].value,
                UnitPrice : selectedRows[i].value


    // alert(event.target.id.split('-'));
    console.log('Access key2:'+event.target.accessKey);
    console.log('name :'+event.target.name);       
        this.olilist[event.target.accessKey].UnitPrice = event.target.value;
        console.log('name 2 :'+event.target.name);  

    else if(event.target.name==='oliQuantity'){
        this.olilist[event.target.accessKey].Quantity = event.target.value;
        console.log('name 2 :'+event.target.name); 

'selectedCons' is working properly. I am accessing it with 'accesskey'. Once deleting the 2nd rows element, getting below error. enter image description here

Please help me in this!

  • First problem is you cannot use id attribute in any custom element ,because they modifyed when rendered .. alternatively you can use data-attributes if you need to get any value from an element
    – vignesh
    May 31, 2020 at 17:45
  • can you show full code so as to reproduce? or else you can create LWC Playground and share it May 31, 2020 at 19:57
  • @vignesh , I am accessing it with 'accesskey'. Can you please guide me how can I achieve this.
    – SFDC 44
    Jun 1, 2020 at 4:00
  • @salesforce-sas I have added Onchange event code. Please have a look and guide me.
    – SFDC 44
    Jun 1, 2020 at 4:02
  • You are setting the values through index. Since you have 2 loops, multiple elements from inner loop will have same access-code. Your implementation is wrong, create another question with actual requirement Jun 1, 2020 at 5:17


You must log in to answer this question.