Source Code: (back to article)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Keyup Event</title>
<style>
.normal { background-color: transparent; }
.active { background-color: lightgreen; }
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
const textArea = document.getElementById('textArea');
textArea.addEventListener('keyup', function(event) {
console.log('Key up:', event.key);
if (event.key === "Control") {
this.classList.remove('active');
this.classList.add('normal');
}
});
textArea.addEventListener('keydown', function(event) {
if (event.key === "Control") {
this.classList.add('active');
}
});
});
</script>
</head>
<body>
<textarea id="textArea" rows="4" cols="50" placeholder="Press and release 'Control' to see the effect"></textarea>
</body>
</html>