How to Detect if CAPS LOCK is ON using JavaScript

How to Detect if CAPS LOCK is ON inside an input field using JavaScript

Step 1 : Create an input field using HTML

<h3>Detect Caps Lock</h3>
<p>Press the "Caps Lock" key inside the input field to trigger the function.</p>

<input id="myInput" value="Some text..">
<p id="text">WARNING! Caps lock is ON.</p>

Step 2 : Add Color to the Warning

#text {display:none;color:red}

Step 3: Add Js to detect if CAPS Lock is ON

var input = document.getElementById("myInput");
var text = document.getElementById("text");
input.addEventListener("keyup", function(event) {

if (event.getModifierState("CapsLock")) {
    text.style.display = "block";
  } else {
    text.style.display = "none"
  }
});

Full Code and Output :

How to Detect if CAPS LOCK is ON using JavaScript
You may Also Like
Scroll to top