To get the ID of the clicked button with onClick with JavaScript, we get the id property.
For instance, we write
const buttons = document.getElementsByTagName("button");
for (const button of buttons) {
button.onclick = (e) => {
console.log(e.target.id);
};
}
to select the buttons with getElementsByTagName.
Then we loop through the buttons with a for-of loop.
In it, we set its onclick property to a function that logs the id of the button being clicked on.
onclick is called when we click on the button.
e.target is the element that trigged the event.