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

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?

PlaygroundPlayground

enter image description here

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)

    }
}

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

enter image description here

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)

    }
}
edited title
Link
Json
  • 1.7k
  • 7
  • 45
  • 93

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

improved Formatting
Source Link
Json
  • 1.7k
  • 7
  • 45
  • 93

I am creating an array of pills and would like to add a data-* attribute to each item. butBut 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

enter image description here

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

enter image description here

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

enter image description here

Source Link
Json
  • 1.7k
  • 7
  • 45
  • 93
Loading