How to use the blur event on text

Viewed 52

Is it possible to use the blur event on a text instead of an input? If so, how do I achieve that using vanilla JavaScript? I've searched a lot but I can't find the answer I'm looking for.

1 Answers

It is possible if you add a tabindex to the text element

var p = document.getElementById("test");

p.addEventListener("focus", function(e) {
  e.target.style.color = "red";  
});

p.addEventListener("blur", function(e) {
  e.target.style.color = "";  
});
<p id="test" tabindex="0">Test</p>

Related