Home > Net >  Is there a way to know which button was pressed without making a new handler in JS?
Is there a way to know which button was pressed without making a new handler in JS?

Time:02-19

I want to use the same button handler for a vanilla JS script. Is there a way to use something like an event handler to get the ID of the button which was pressed, and pass that into the subroutine?

Pseudocode to get the point across

document.addEventListener('buttonClick', (event) => {
    drawComponent(event.clientX, event.clientY, event.buttonID);
})

I'm using ASP.Net MVC, electron, and the Razor View Engine if that's relavent.

CodePudding user response:

Use event.target

document.addEventListener('buttonClick', (event) => {
    console.log("element: "   event.target);
    drawComponent(event.clientX, event.clientY, event.buttonID);
})
  • Related