Created
February 1, 2015 20:02
-
-
Save greggnakamura/d806336d8c0cebdc5f7e to your computer and use it in GitHub Desktop.
Javascript: DOM level 0; Event Listeners (IE Legacy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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