An example demonstrating the use of event listeners (used as one-offs or globally) in Javascript. When using event listeners globally you can use event.target.matches
to "listen" for events executed on all HTML elements (identified bij element, class or ID)
Preview at: bl.ocks.org
Last active
January 31, 2022 21:53
-
-
Save davidvandenbor/a683335eae6e4caadc5b70a4417efb85 to your computer and use it in GitHub Desktop.
Event Listeners in javascript
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="style.css"> | |
<title>Document</title> | |
</head> | |
<body> | |
<h1>Javascript events</h1> | |
<p>mouseovers and mouseclicks!</p> | |
<button class="hallo">Hallo</button> | |
<button class="totziens">Tot ziens</button> | |
<section class="notitie"></section> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
/* separate event listeners ---------------------------- | |
*/ | |
document.addEventListener('mouseover', function (event) { | |
if (!event.target.matches('.hallo')) return; | |
document.querySelector(".notitie").textContent = "De muis is OVER Hallo!"; | |
}); | |
document.addEventListener('mouseover', function (event) { | |
if (!event.target.matches('.totziens')) return; | |
document.querySelector(".notitie").textContent = "De muis is OVER Tot ziens!"; | |
}); | |
/* combined event listeners ---------------------------- | |
*/ | |
document.addEventListener('click', function (event) { | |
if (event.target.matches('.hallo')) { | |
document.querySelector(".notitie").textContent = "Je hebt GEKLIKT op: Hallo!"; | |
return; | |
} | |
if (event.target.matches('.totziens')) { | |
document.querySelector(".notitie").textContent = "Je hebt GEKLIKT op: Tot ziens!"; | |
return; | |
} | |
}); |
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
body { | |
margin: 0 auto; | |
padding:1%; | |
/* max-width:400px; */ | |
font-family: sans-serif; | |
} | |
button { | |
position:relative; | |
background:green; | |
color:white; | |
padding:20px 40px; | |
font-size:30px; | |
border-radius:10px; | |
cursor:pointer; | |
transition: all .2s ease; | |
} | |
button:hover { | |
background: #000; | |
} | |
.notitie { | |
background-color: lightgrey; | |
min-height:100px; | |
padding: 2%; | |
display:grid; | |
width:100%; | |
text-align: center; | |
align-items: center; | |
margin-top: 20px; | |
border-radius:10px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment