ASP.NET CSS Highlight TextBox on Focus
Many sites have a useful visual way to show the user what TextBox (input text control) is currently focused or selected by highlighting the background of the control with a color such as yellow. When you tab or click into the TextBox, the border and background usually change to denote it has focus.
Here is what the TextBox should look like when it is focused and unfocused after applying this trick:
Un-Focused TextBox

Focused TextBox

There is an easy CSS trick that will get this working in Firefox and Safari with no need for Javascript but unfortunately, IE 6 & 7 do not support this CSS instruction. So we have to add a bit of javascript to make this work. First, add the CSS class that you want to use when the control has focus and when it is unfocused. Here are a couple of examples of CSS properties to use (NOTE: You can add this code to your page's HEAD tag or to your external CSS style sheet):
1: /* This is the CSS class to use when no focus is on the input control */ 2: .input_text 3: { 4: border:1px solid #c0c0c0; 5: padding:4px; 6: font-size:14px; 7: color:#000000; 8: background-color:#ffffff; 9: } 10: 11: /* This is the CSS class to use when the control has focus */ 12: .input_text:focus, input.input_text_focus 13: { 14: border-color:#646464; 15: background-color:#ffffc0; 16: }
Next, here is the Javascript that you can use to attach an "onFocus" and an "onBlur" event to all input controls on your page that match certain conditions. Add this Javascript to your page's HEAD tag or in an external .js file:
1: // creates a function to set the css class name of input controls when they
2: // receive or lose focus.
3: setAspnetTextFocus = function() {4: // CSS class name to use when no focus is on the input control
5: var classBlur = 'input_text';
6: // CSS class name to use when the input control has focus
7: var classFocus = 'input_text_focus';
8: // get all of the input tags on the page
9: var inputElements = document.getElementsByTagName('input');
10: 11: for (var i = 0; i < inputElements.length; i++) {
12: // add the onfocus event and set it to add the on focused CSS class
13: inputElements[i].onfocus = function() {14: if (this.className == 'input_text') {
15: this.className += ' ' + classFocus;
16: } 17: }18: // add the onblur event and set it to remove the on focused CSS class when it loses focus
19: inputElements[i].onblur = function() {20: this.className = this.className.replace(new RegExp(' ' + classFocus + '\\b'), '');
21: } 22: } 23: }24: // attach this event on load of the page
25: if (window.attachEvent) window.attachEvent('onload', setAspnetTextFocus);
Now, add a TextBox control to your page with the following attributes:
1: <!-- This text box will highlight when it receives focus. -->
2: <asp:TextBox ID="TextBox1" runat="server" CssClass="input_text"></asp:TextBox>
Now build and run to try out your new TextBox. I have tested this in Firefox 2, Safari 2, and IE 6 & 7.
Popular Articles
Recent comments
- jkll
12 hours 50 min ago - Thank You
1 day 12 hours ago - Another approach
3 days 13 hours ago - Issue
4 days 3 hours ago - thanks
4 days 13 hours ago - Calendar date time
4 days 19 hours ago - Nice Explanation
4 days 23 hours ago - ramya
1 week 20 hours ago - thank a lot
1 week 1 day ago - Very useful, but...
1 week 4 days ago

Date of the article
Where is the date of the article?
css on focus is not working
i don't under stand this input.input_text_focus line
Thank youu..soo much
Hi..
This solved my problem ..
thanks a lot..
god bless u.
Doesn't work on my IE8
Not working with Javascript or without!
change back ground color of text box on focus
It doesn't work when the page post backs
it's worked
put this code in you page_load
RegisterStartupScript("textOnFocus", "javascript:setAspnetTextFocus();");
Javascript is still there
Mr anonymous you mislead my google search with your stupid post !!
ie8 works fine without javascript
excellent work,
it works very good !
thanks
Thanks
Works beautifully! Tried it with IE tester.
Thanks
I used it and it worked just right with IE 8.