Daryl
Daryl

Reputation: 25

How to use the blur event on text

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.

Upvotes: 2

Views: 294

Answers (1)

biberman
biberman

Reputation: 5777

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>

Upvotes: 3

Related Questions