Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing text background with javascript... 2

Status
Not open for further replies.

TomTT

Programmer
Jun 3, 2002
55
US
This shouldn't be difficult, but I'm having problems after trying styles, class, etc.

I have a input type=text and I want to change the background color based on the option button selected by the user. The option button calls a javascript with an if statement. This is, logically where I would change the text background color.

I can get as far as:

"document.frm1.txtMessage" but can find the correct access to the background color property.

I tried writing a sytle class that set background-color then set document.frm1.txtMessage.class = class-name

No good.

Direct calls like document.frm1.txtMessage.style doesn't work either.

What kind of syntax do I need to access and change this property?

Thanks for any suggestions.

Tom T
 
Assuming you have an id=&quot;txtMessage&quot; in the <input> tag, it should be this:

document.getElementById('txtMessage').style.backgroundColor = &quot;red&quot;;

 
Thanks for both the responses.

Tusharsawant, your suggestion works as written if you change bgcolor to backgrounColor.

Starway, your suggestion worked as written.

I found the reference to javascript's backgroundColor last eventing. With HTML's bgColor property and the CSS style=&quot;background-color&quot; attribute, I was surprised to find yet another method to change the background color.

Thanks for you quick responses.

Tom T
 
No surprise,

&quot;background-color&quot; is the syntax for CSS
while
&quot;backgroundColor&quot; is the syntax for javascript.

Here you'll find a list of CSS properties that can be changed dynamically, with corresponding js equvalents:
(see &quot;Object CSS2Properties&quot; at the bottom)
 
Just a word of caution...

Background colors in text input doesn't work with NS 4.79 and earlier. There's always a better way...
 
Yes, NN4.x doesn't support this for form elements. Nor Opera does (yet), as it isn't required by any standard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top