I have a ASP text box control. When the user focuses on text box, i want to change the background color of the text box from gray to white.
here is the css file, but its not changing the color after focusing on the text box.
<script language="javascript" type="text-javascript">
function DoFocus(txt)
{
txt.className = 'focus';
}
</script>
Textbox
<asp:TextBox ID="txtFirstName" runat="server"
CssClass="textbox" MaxLength="50" Width="188px" onfocus="DoFocus(this)">
CSS
input.textbox, select, textarea
{
font-family : verdana, arial, snas-serif;
font-size : 11px;
color : #000000;
padding : 3px;
background : #f0f0f0;
border-left : solid 1px #c1c1c1;
border-top : solid 1px #cfcfcf;
border-right : solid 1px #cfcfcf;
border-bottom : solid 1px #6f6f6f;
}
input.textbox:focus, input.input_text_focus
{
border-color:#646464;
background-color:#ffcf03;
}
EDIT: I saw you updated your post, so to clarify: ASP creates an input
HTML element (correct me if I'm wrong) and you can always style this via the :focus
selector in CSS, no need for Javascript, but also add input.textbox:active
to catch some buggy IE...
input.textbox:focus, input.textbox:active {
/* everything you put here will be aplied to ANY focused input element */
}
Judging from your pasted code, instead of
.input_text:focus, input.input_text_focus {
border-color:#646464;
background-color:#ffffc0;
}
use
input.textbox:focus, input.input_text_focus {
...
}
Or why do you suddenly use the class input_text
when you have input.textbox
in the firsthand? Your two selectors don't match...
Here an approach with the use separated CSS classes specified via javascript:
<style type="text/css">
input.FocusedStyle
{
background-color:#ffffc0;
border-color:#646464;
}
</style>
<script type="text/javascript">
function OnBlur(textBox) {
textBox.className = '';
}
function OnFocus(textBox) {
textBox.className += ' FocusedStyle';
}
</script>
<asp:TextBox ID="txt" runat="server" onblur="OnBlur(this);" onfocus="OnFocus(this);"></asp:TextBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With