M
M
maxon_by2018-02-01 15:39:38
JavaScript
maxon_by, 2018-02-01 15:39:38

What is the difference between .addEventListener() and .onclick = function() in JS?

What is the difference between .addEventListener() and .onclick = function() in JS?
and
Why is .addEventListener() used more often now
Maybe these are different versions?
Thanks everyone for the replies!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OKyJIucT, 2018-02-01
@maxon_by

addEventListener adds a listener for any event you specify , and onclick only listens for clicks. Therefore what to use - depends on the task.
In addition, the second method allows you to hang only one event handler (the next one will overwrite the previous one), while the first one - as many as you like.

W
Well here again 25, 2018-02-01
@TT55EE

.onclick is sometimes more convenient for the programmer - no need to call the constructor, there is already a ready-made property,
compare the pseudocode:

button.onclick = handler_function
button.add_onclick_function( onclick_type, handler_function )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question