Javascript: onchange or addEventListener?

I am completely new to Javascript. But I learned a little about Activescript. I was taught to use addEventListener to handle events.

But when I come to JavaScript, although addEventListener is an option, it seems like this is a small way to use addEventListener. Instead, people use onChange ().

Besides syntactically, are there behavioral differences?

I was also taught to always use removeEventListener. This does not seem very necessary. If I use onChange, is there no need for removeEventListener code?

thank

+5
source share
3 answers

Two main differences:

+3

addEventListener . onchange.

+2

I personally think addEventListener is much better. An example you can do with this in HTML5

document.addEventListener("input", function(){
  target = window.event.target.value;
  window.event.target.nextSibling.nextSibling.value = target*2;
});
<input name="1" value="25">
<input name="2" value="50" readonly>
<p>Hello World</p>
Run codeHide result
+1
source

All Articles