Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  162] [ 7]  / answers: 1 / hits: 24144  / 13 Years ago, wed, november 30, 2011, 12:00:00

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


More From » asp.net

 Answers
4

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



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



enter



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.


[#88823] Tuesday, November 29, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyonnaelled

Total Points: 35
Total Questions: 113
Total Answers: 99

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
keyonnaelled questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Wed, Oct 14, 20, 00:00, 4 Years ago
Tue, Sep 8, 20, 00:00, 4 Years ago
Sat, Jul 11, 20, 00:00, 4 Years ago
;