Last active
August 27, 2016 23:26
-
-
Save mattConn/a09fd172041a464eeae2c096acc9cba3 to your computer and use it in GitHub Desktop.
Get any element in a document on click, hover, etc. Can access properties of "event.target" like you would typically access an element's properties, i.e. "event.target.style".
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
// getting an event's target | |
function getElementByEvent(event){ | |
console.log(event.target); | |
} | |
// get the target | |
var element = document.getElementsByClassName('element')[0]; | |
//adding event listener to target | |
element.addEventListener('click', function(event){ | |
getElementByEvent(event); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've named this Gist as I have because this is an easy way to allow for the user to click anywhere and via conditional, check if a certain reaction should occur.
Demo:
http://codepen.io/mattConn/pen/PzrKKJ?editors=1111