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
Shreya Singh 36Shreya Singh 36 

Can anyone tell me how can I handle user interactions like button clicks in LWC?

Best Answer chosen by Shreya Singh 36
Arun Kumar 1141Arun Kumar 1141
Hello Shreya,

To handle this, you can follow below steps:
  • Add button in HTML file.
<button data-id="myClick" onclick={handleMyClick}>Click on Me</button>
  • Add LightingElement in your JS file to handle button click event
import { LightningElement } from 'lwc';

export default class MyClass extends LightningElement {
    handleMyClick(event) {
        // Write your logic here
    }
}

Mark it best answer, if it helps you.
Thanks

All Answers

Arun Kumar 1141Arun Kumar 1141
Hello Shreya,

To handle this, you can follow below steps:
  • Add button in HTML file.
<button data-id="myClick" onclick={handleMyClick}>Click on Me</button>
  • Add LightingElement in your JS file to handle button click event
import { LightningElement } from 'lwc';

export default class MyClass extends LightningElement {
    handleMyClick(event) {
        // Write your logic here
    }
}

Mark it best answer, if it helps you.
Thanks
This was selected as the best answer
Shreya Singh 36Shreya Singh 36
Thanks @Arun