Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Created February 1, 2015 20:02
Show Gist options
  • Save greggnakamura/d806336d8c0cebdc5f7e to your computer and use it in GitHub Desktop.
Save greggnakamura/d806336d8c0cebdc5f7e to your computer and use it in GitHub Desktop.
Javascript: DOM level 0; Event Listeners (IE Legacy)
(function () {
// HTML items
// <button>Normal</button>
// <button>Changed</button>
// <a href="http://www.google.com">Google</a>
// <a href="http://www.google.com">Google</a>
var buttons = document.getElementByTagName('button');
var links = document.getElementByTagName('a');
for (var i = 0; len = buttons.length; i < len; i = i + 1) {
// set class on body element when button is clicked
// DOM Level 0 events
buttons[i].onclick = function () {
var className = this.innerHTML.toLowerCase();
document.body.className = className;
};
// listen for 'a' click event
links[i].addEventListener('click', function (e) {
var className = this.innerHTML.toLowerCase();
e.preventDefault();
document.body.className = className;
});
// IE legacy click events
links[i].attachEvent('onclick', function (e){
var className = event.srcElement.innerHTML.toLowerCase();
event.returnValue = false;
document.body.className = className;
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment