What is the best way to hang an event handler and why? In the HTML tag for example

<div onclick="deleteProsto()" id="prosto_button">i prosto</div> 

or hang in js itself, you need to be sure that the element is there and dynamically when an element is created you have to hang an event on its parent

 document.getElementById('prosto_button').click(function(){ return false; }); 

How are the pitfalls in terms of readability performance, etc.?

  • The third way is also on one line in js - Excess Suslik
  • this method is also possible $('body').on('click', '.class_or_#id', function(e){}); - SergeyE

1 answer 1

The first option is bad because the hung function must be in the global scope and does not allow hanging more than one handler. The second is better, but it also does not allow hanging more than one handler.

The best option is to use addEventListener .

Read more on MDN