Skip to content

Instantly share code, notes, and snippets.

@dhunmoon
Created July 21, 2023 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhunmoon/b0cb7816fe857bdc4e1bdd2b601cf101 to your computer and use it in GitHub Desktop.
Save dhunmoon/b0cb7816fe857bdc4e1bdd2b601cf101 to your computer and use it in GitHub Desktop.
Adding event to an element (backward compatable)
/**
* @param {Object} el Element where the event needed to be added
* @param {string} type Type of event which is getting added
* @param {function} handler Event handler which will get called.
*/
function addHandler(el, type, handler) {
//overwrite the existing function
if (el.addEventListener){ //DOM2 Events
addHandler = function(el, type, handler) {
el.addEventListener(type, handler, false)
};
} else { //IE
addHandler = function(el, type, handler) {
el.attachEvent("on" + type, handler)
};
}
//call the new function
addHandler(el, type, handler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment