Click here to Skip to main content
14,978,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have around 10 to 15 textboxes on my page and they are all required. Can any one tell me how to change there background color onFocus of them? I can do it by using getElementById, but I don't want to fetch every single id. I just want to send the control (say textbox or dropdownlist) to the function and change that controls background color.


It'll be very great if someone help me on this. Thanks in advance..

Thanks
Yogesh
Posted

Create a JavaScript function which will take ControlID as an argument.
In the function, set the background color of the control whose ControID has been passed.
Call the function on onfocus of the required textboxes passing its id.
   
Comments
Yogesh Gulve 14-May-10 5:35am
   
thanks Mr.ankur for your valuable reply.
Hey Friends,

I got the answer for this.

Add the following 2 functions in block on aspx page.

<pre lang="javascript">&lt;script language="javascript" type="text/javascript">

function DoBlur(fld)
{
fld.style.background="#FFFFFF";
}

function DoFocus(fld)
{
//fld.className = 'focusfld';
fld.style.background = "lightyellow";
}

&lt;/script></pre>

And on page apply these functions to textbox's or dropdownlist's OnFocus and OnBlur properties just like follows.

<pre lang="xml">&lt;asp:textbox id="txt1" runat="server" onfocus="DoFocus(this);" onblur="DoBlur(this);" /&gt;</pre>

Whenever you focus on that control it'll change the color of that control. Following code will focus the textbox when it is not validate.
<pre lang="vb">
Try
'Validation of textbox will go here
Catch ex As Exception
txt1.Focus()
Throw ex
End Try</pre>


Hope this code will help everyone who required this kind of functionality.

Thanks,
Yogesh
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900