Take a look at the CSS :focus selector. And just specify the CSS class, for example:
#txt1:focus { border-color:red; }
Since it is not supported by older browsers (IE7 plus some others). You can do this with JS.
$("#txt1").focus(function(){ $(this).addClass("focused")}); $("#txt1").blur(function(){ $(this).removeClass("focused")});
Where focused is defined as follows:
#txt1.focused { border-color:red; }
EDIT:
Jquery.focus docs.
$("#txt1").focus(function(){ $(this).addClass("focused")});
The line above simply binds the handler to focus the event and does not change focus or execute it immediately. To get a field focused on page loading, add the following line after it:
$("#txt1").focus();
This parameter will focus on the selected element and start the handler attached earlier. Shorter designations:
$("#txt1").focus(function(){ $(this).addClass("focused")}).focus();
Demo with autofucused element on page load
source share