if I have 10 buttons, am I better to have an event listener that points to each button and calls a different function:
hdlButton1.addEventListener("click", fButton1Clicked);
hdlButton2.addEventListener("click", fButton2Clicked);
etc.
function fButton1Clicked(event) {
//code to execute upon button 1 click
}
function fButton2Clicked(event) {
//code to execute upon button 1 click
}
Or have all the clicks point to a single function:
document.addEventListener("click", fButtonClick);
function fButtonClick(event) {
switch (event.target.id) {
case "btn1":
//code to handle button 1 click
break;
case "btn2":
//code to handle button 2 click
break;
}
}
What are the implications for performance in having multiple functions to handle the various events versus having a single function that differentiates between the events and handles appropriately?
(Thanks)
onclickHTML attribute adds an event listener. If you meant heonlcickJS property on DOM nodes, then that's still adding an event listener. The only thing is that you cannot add more than one event listener via this route.