Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created October 21, 2017 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/ee2ff700079760d8f7ffc2a5bc249009 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/ee2ff700079760d8f7ffc2a5bc249009 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>handleEvent method</title>
<meta name="viewport" content="width=device-width">
<!-- -->
<style>
</style>
</head>
<body>
<h1>Built-In EventListener handleEvent method</h1>
<button id="btn">Click the Button</button>
<script>
let obj = {
init: function (){
document.querySelector('#btn').addEventListener('click', this);
document.querySelector('#btn').addEventListener('focus', this);
document.querySelector('#btn').addEventListener('blur', this);
},
handleEvent: function(ev){
switch(ev.type){
case 'click':
this.something(ev);
break;
case 'focus':
this.something(ev);
break;
case 'blur':
this.something(ev);
break;
case 'explode':
break;
}
},
something: function (ev){
//gets called by click event list
console.log('btn was', ev.type, '-ed.');
}
}
//get things started
obj.init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment