The focus event runs when an input, button, link, or another focusable control receives focus. It is useful for field guidance and accessible form feedback.
$( selector ).on( "focus", function (event) {
// The element has received focus.
});
$( function () {
$( "#t1" ).on( "focus", function () {
$( "#d1" ).text( "Enter your name" );
});
$( "#t2" ).on( "focus", function () {
$( "#d1" ).text( "Enter your email" );
});
});
Pair this topic with blur, which runs when focus leaves a control.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.