Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are event listeners identical to html events?

Is onclick in HTML identical to .click() in jQuery?

Obviously they are identical from a usability standpoint, but I am curious how the browser handles them.

like image 653
devnill Avatar asked Jan 18 '23 20:01

devnill


1 Answers

jQuery attaches events using JavaScript behind the scenes.

An important difference is that jQuery allows multiple events to be bound using click(), where as you can only attach one handler using onclick. This is because jQuery uses either addEventListener or attachEvent behind the scenes, as opposed to binding to .onclick directly.

Furthermore, attaching handlers via JavaScript (using jQuery or not) promotes unobtrusive JavaScript (i.e. not mixing JavaScript and HTML), which is a good thing.

like image 109
Matt Avatar answered Jan 22 '23 02:01

Matt