Skip to content

Instantly share code, notes, and snippets.

@mattConn
Last active August 27, 2016 23:26
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 mattConn/a09fd172041a464eeae2c096acc9cba3 to your computer and use it in GitHub Desktop.
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".
// 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);
});
@mattConn
Copy link
Author

mattConn commented Aug 27, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment