Skip to main content
4 of 4
added 1 character in body
Json
  • 1.7k
  • 7
  • 45
  • 93

Dynamically add data-* attribute to an item inside an array

I am creating an array of pills and would like to add a data-* attribute to each item. But unfortunately I get a syntax error.

I tried changing the field name 'data-id' to 'dataId' but still wasn't able to query the field using event.target.dataset.dataId.

What is the right syntax for this?

Playground

import { LightningElement, track } from 'lwc';

export default class App extends LightningElement {

    @track items = [

    ];
    handleClick(event){
        let obj ={
            label: 'test', 
            data-Id: 'my data id' <---- This is where I get syntax error
        }           
        this.items.splice(this.items.length,0,obj)  //adding new items to array
    }  
    copyPillToForm(event){
        console.dir(event.target.dataset.data-Id)

    }
}
Json
  • 1.7k
  • 7
  • 45
  • 93