[thelist] using JS to alter background colour of form input

Duncan O'Neill dbaxo at ihug.co.nz
Sun Mar 9 11:38:01 CST 2003

Dunstan Orchard wrote:
> Hi there,
> I have this little script that checks to see if a form field has been filled
> in, and if not it alerts the user then puts focus back into that field.
> e.g.
> if (document.getElementById('FirstName').value == "")
> 	{
> 	alert("Please enter a value for the \"First name\" field.");
> 	document.getElementById('FirstName').focus();
> 	return (false);
> 	}
> Is there any way I can also alter that form input's CSS background colour
> using JS to draw their attention to it?
> All the things I've tried, like:
> document.getElementById('FirstName').style.background-color.value = "#d6d";
> have no effect, or break the script.


there are a couple of syntax errors in the above line which will
stop it from working.

*background-color should be backgroundColor
*you don't need 'value';
*you have to separate the '#' string from the hex number, and use
  the six-digit version.

Something in this form works in IE6, Moz 1.0.1, and Opera 7.0;

document.getElementById('FirstName').style.backgroundColor='#' + '000000';


Duncan O'Neill

More information about the thelist mailing list