0

I am trying to change background color of textbox by using javascript but my code is not working. I search SO but not find any suitable answer. Here is my code.

<head>
    <script type="text/javascript" language="javascript">
        function abc() {
            var v = document.getElementById("<%=TextBox1.ClientID%>");
            v.setAttribute('BackColor', 'Red');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="abc()" onclick="Button1_Click"/>
    </div>
    </form>
</body>

enter image description here

1
  • How about v.style.backgroundColor="red"; ? Commented Nov 30, 2011 at 16:40

2 Answers 2

7

You are trying to change the UI that is html, so you need to use javascript/css properties.

Here there is a list of css attributes accessible by javascript.

Try with:

   <script type="text/javascript" language="javascript">
        function abc() {
            var v = document.getElementById("<%=TextBox1.ClientID%>");
            v.style.backgroundColor = "red";
        }
    </script>

Furthermore I have the Visual Studio 2010 and the Intellisense also show me the style attribute:

enter image description here

You are right jams, when I pretend to point to an id from a html generated by asp the intellisense doesn't works for the style attribute:

enter image description here

enter image description here

I think the Intellisense doesn't reach to this id because at the moment of you are writting this code, the html doesn't exists.

Sign up to request clarification or add additional context in comments.

7 Comments

I check it in Visual Studio 2010, but when I type v dot then intellisense windows does not has style option.
@jams It's so weird my Visual Studio 2010 does show me the style option. I think could be the "<%=TextBox1.ClientID%>", because when I write a document.getElementById("existingId") and the existingId are in the html aspx file does behaves like I said.
can you post a screen shot for verification. I am posting my.
I have posted my. please check it.
you are using <p id="texID" and I am using <asp:TextBox ID="TextBox1" runat="server">. When you will try TextBox you will not get style option. Check it and tell me.
|
2

BackColor does not exist on the client side. That's an ASP.NET server-side notion. Rather, you want to set it with CSS:

v.style.backgroundColor = 'Red';

Here is a reference of the names of CSS properties as they would appear in JavaScript.

3 Comments

yes it works. I am using Visual Studio 2008 and its intellisense windows does not show style for v. Can you explain it why?
@jams I guess that is not registered in 2008, but in Visual Studio 2010 does.
@jams Galled is right, 2008 has very limited support for JavaScript intellisense. If you really want it, add ins like Resharper 6 + add it in, better than VS 2010 does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.