0

After reading this comment, I changed the way I handled specific events in my application.

However, I ended up with a lot of code that seems "similar", and I was wondering if there's a way to group multiple event listeners that happen on the same parent.

Here's an example of 3 different buttons that submit some form. The buttons are added dynamically and not all of them might be available all the time.

const parentElement = $('#myBlock')
parentElement.on("submit", "form[data-action='button-1']", (evt) => {
  //do something
});

parentElement.on("submit", "form[data-action='button-2']", (evt) => {
  //do something else
});

parentElement.on("submit", "form[data-action='button-3']", (evt) => {
  //do magic
});

Is there a better way of handling this?

Erythros
  • 123
  • 9

0 Answers0