views:

1068

answers:

3

Hi

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 fied (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?

Thanks in advance

+6  A: 

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

cobbal
That's a strange name for the JS Event, but it works. Thanks
it is a little odd, but it does match up with the other events (onclick, onkeyup, onchange, etc.)
cobbal
@PlezHelp: you should not read "onfocus" as "when 'focus' becomes 'on'", but as "when the 'focus' event happens, do ..". So 'onblur' becomes "when the blur event happens, do.." (and then you need to know that "blur" is the opposite of "focus")
Hans Kesting
+1  A: 

Use the onBlur event:

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

This works great. Thanks man =)