GGSoft GGSoft - 5 months ago 39
ASP.NET (C#) Question

Change BackgroundColor of ASP:TEXTBOX via javascript

I have declared asp:textbox in my aspx page.

<asp:TextBox ID="txta" runat="server" size="12" value="123"></asp:TextBox>

and I have a javascript function in HEADER:

<head runat="server">
<title>My Page</title>

<script type = "text/javascript" >

function changeColor() {
document.getElementById('<%= txta.ClientID %>').backgroundColor="#FF0000";
alert( '<%= txta.ClientID %>');


on Page_Load event I joined onBlur attribute with asp control like this:

txta.Attributes.Add("onblur", "changeColor();")

When control losts focus, alert is shown, but backgroundcolor doesn't changed.

How to achieve this?


Use document.getElementById('<%= txta.ClientID %>').style.backgroundColor = "#FF0000";.

You forgot the .style.