Last active
April 3, 2022 16:48
-
-
Save lucywoodman/4545135f42f1d1321ada473022e60a0a to your computer and use it in GitHub Desktop.
Event listeners
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
{ | |
"scripts": [], | |
"styles": [] | |
} |
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> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>repl.it</title> | |
<link href="style.css" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
<button id="magic-button">Click me!</button> | |
<div id="magic-div"> | |
<p>Mouse over me!</p> | |
</div> | |
<input id="magic-input" type="text" placeholder="Type something into me"> | |
<script src="script.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
function handleBtnClick(event) { | |
console.log('Received the ' + event.type + " event!"); | |
console.log('"this" currently refers to', this.id); | |
console.log('You clicked the button!\\n'); | |
} | |
function handleInputKeys(event) { | |
console.log('Received the ' + event.type + " event!"); | |
console.log('"this" currently refers to', this.id); | |
console.log('You changed the textbox!\\n'); | |
} | |
function handleDivMouseOver(event) { | |
console.log('Received the ' + event.type + " event!"); | |
console.log('"this" currently refers to', this.id); | |
console.log('You moved the mouse over the div!\\n'); | |
} | |
let magicButton = document.getElementById('magic-button'); | |
let magicDiv = document.getElementById('magic-div'); | |
let magicInput = document.getElementById('magic-input'); | |
magicButton.addEventListener('click', handleBtnClick); // left clicks | |
magicButton.addEventListener('contextmenu', handleBtnClick); // right clicks | |
magicDiv.addEventListener('mouseover', handleDivMouseOver); // mouseovers | |
magicInput.addEventListener('keypress', handleInputKeys); // key presses | |
magicInput.addEventListener('keydown', handleInputKeys); // key down | |
magicInput.addEventListener('keyup', handleInputKeys); // key up |
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
div { | |
margin-top: 25px; | |
margin-bottom: 25px; | |
padding: 10px; | |
height: 100px; | |
width: 250px; | |
background-color: lightblue; | |
border: 1px solid black; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment