PlezHelp
PlezHelp

Reputation:

JavaScript - OffFocus event?

What I want to do is display an input field with the text color black.

Then when the person clicks inside the input field (onfocus) I want to change the text color to red.

Then, when the person click outside of the input field (no longer focus), I want to change the text color back to black.

I know how to handle the JavaScript onfocus event using the following:

<input ctype="text" onfocus="this.style.color='red';" />

But how do I handle the "off focus" to change the text color back to black?

Upvotes: 13

Views: 26892

Answers (3)

cobbal
cobbal

Reputation: 70785

try using onblur, it is called when an element loses focus.

Upvotes: 26

Alex Rozanski
Alex Rozanski

Reputation: 38015

Use the onBlur event:

<input ctype="text" onfocus="this.style.color='red';" onblur="this.style.color='black';"/>

Upvotes: 3

yincrash
yincrash

Reputation: 6484

rather than fire some JS on the event, you can use css selectors.

#someInput:focus { color: red; } #someInput { color: black; }

will do the same thing without javascript.

Upvotes: 2

Related Questions