How do I update addEventListener?

I am going to update my code to use addEventListener () only for writing to an element property in javascript.

Before I do this, I would like to check a few things.

1.) I assume that I do not need to call removeEventListener () if I update the DOM and remove the elements (by .innerHTML entry).

2.) addEventListener is supported in popular modern browsers - IE9, Chrome, Firefox, Safari

3.) There are no other problems that can occur in modern browsers.

I ask bc I do not want to jump with a gun when updating the code.

Notes:

to correlate events (delete on).

  • onkeypress - keypress
  • onblur → blur
  • onfocus → focus

Study

https://developer.mozilla.org/en/DOM/element.addEventListener (there is a compatibility table)

http://www.quirksmode.org/js/events_advanced.html

JavaScript, "keypress" backspace?

  • false. preventDefault(), .
+5
2
  • . DOM (, ), .

  • . IE attachEvent(), .

  • , . .

: jQuery -. addEventListener, , attachEvent IE, , . DOM- .

+3

1) removeEventListener, removeEventListener , DOM, eventListener, addEventListener, .

2) addEventListener

3) , addEventListener

4) onkeypress - , , - . *

, jQuery - :

Element.prototype.on = function(evt, fn) {
if (this.addEventListener)
    this.addEventListener(evt, fn, false);
else if (this.attachEvent)
    this.attachEvent('on' + evt, fn);
};

Element.prototype.off = function(evt, fn) {
    if (this.removeEventListener)
        this.removeEventListener(evt, fn, false);
    else if (this.detachEvent)
        this.detachEvent('on' + evt, fn);
};
+4

All Articles