It's not a secret anymore!
The moose likes HTML, CSS and JavaScript and the fly likes How to color a blank textfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to color a blank textfield" Watch "How to color a blank textfield" New topic

How to color a blank textfield

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
I am using some JSP but I want a textfield colored red if it is blank. If this can be done in Javascript, please let me know. Any help is greatly appreciated.


if the textfield has no value, color it red. if user inputs a value then take color out.

<td><input name="textfield1" type="text" size="30" value= "<%=N_ASDR1 %>"></td>
<td><input name="textfield2" type="text" size="30" value= "<%=N_ASDR2 %>"></td>
<td><input name="textfield3" type="text" size="30" value= "<%=N_ASDR3 %>"></td>
<td><input name="textfield4" type="text" size="30" value= "<%=N_ASDR4 %>"></td>
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
{bear edit: removed example too full of errors to be useful. Niki feel free to re-post once you fix the problems]
[ October 03, 2005: Message edited by: Bear Bibeault ]

Life called,so here I am.<br />Cheers<br />Niki.:-)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

You can color the background with the background-color CSS property.

For changing based upon whether the field is empty or not, you will need to establish event handlers on the text field to detect changes. The onchange handler itself will not be enough because it's not called on individual keystrokes. You'll need to also at least listen to onkeyup.

In the handler you can change the color of the background using CSS under Javascript control.

It's not foolproof however. I'm not sure how you can capture the field being emptied by a cut operation via the menu for example.

[ October 03, 2005: Message edited by: Bear Bibeault ]
[ October 03, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: How to color a blank textfield
jQuery in Action, 3rd edition