Created
December 16, 2016 19:11
-
-
Save tylernchls/7c06ce48dc5cc42346a5e58bf4aed8eb to your computer and use it in GitHub Desktop.
Example click events using 'this'
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Event Listeners</title> | |
</head> | |
<body> | |
<h1>Events</h1> | |
<div id="btnBox"></div> | |
<script type="text/javascript" src="events.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
var btnBox = document.getElementById(('btnBox')); | |
for (var i = 0; i < 5; i++) { | |
var btn = document.createElement('button'); | |
btn.id = 'btn' + i; | |
btnBox.appendChild(btn); | |
//add event listener | |
btn.addEventListener('click', function() { | |
console.log(this.id); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment