jQuery - blur() / focus()

The jQuery blur() and focus() methods attach to an event handler function to an HTML form field. The function is executed when the form field gains or loses focus.

 

 


// jQuery method
$("#txtName1").focus(function () { $(this).css("background-color", "#cccccc"); });
$("#txtName1").blur(function () { $(this).css("background-color", "#ffcc00"); });

//fyi - javascript method
document.getElementById("txtName2").addEventListener("focus", function () { this.style.background = "#ff0000"; });
document.getElementById("txtName2").addEventListener("blur", function () { this.style.background = "#00ff00"; });