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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically assigning CSS Style 1

Status
Not open for further replies.

Annelies

Programmer
Nov 25, 2001
72
Hi

I want to be able to change the CSS Style of a textbox based on an event (in this case, the user clicking a checkbox).

Can I do this? I'm using a javascript function (but am not a javascript guru, so don't know if I'm missing the obvious!).

Thanks for your help!!

Annelies =)

 
Assuming that your textbox looks like this:

<input type=&quot;text&quot; id=&quot;txtBox&quot;>

You can change the style like this:

<script language=&quot;javaScript&quot;>
function changeStyle()
{
document.getElementById('txtBox').style.color = &quot;red&quot;;
}
</script>

For your checkbox, just call changeStyle() with the event &quot;onclick&quot;: onclick=&quot;changeStyle()&quot;

Hope that helps.
 
Hmmm

I can't seem to get that to work...

I get an error message &quot;Object doesn't support this property or method&quot;

Any other ideas gratefully appreciated!!!

Thanks

Annelies
 
There aren't a ton of things you can do with the style of a textbox, but here's one of the most obvious. This calls the JavaScript function based on the cursor's leaving the textbox. But its behaviour would be the same if triggered by the &quot;onclick&quot; event of the checkbox.

<html>
<body>
<script language=&quot;javaScript&quot;>
function changeStyle()
{
document.getElementById('txtBox').style.backgroundColor = &quot;red&quot;;
}
</script>
<input type=&quot;text&quot; name=&quot;T1&quot; id=&quot;txtBox&quot; size=&quot;20&quot; onblur=&quot;changeStyle()&quot;>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top