The blur event runs when a focused element loses focus. It is often used for lightweight validation after the user leaves a field.
$( selector ).on( "blur", function (event) {
// The element has lost focus.
});
$( function () {
$( "#t1" ).on( "blur", function () {
const value = $( this ).val().trim();
if ( value.length === 0 ) {
$( "#d1" ).text( "Please enter your name" );
}
});
$( "#t2" ).on( "blur", function () {
const value = $( this ).val().trim();
if ( value.length === 0 ) {
$( "#d1" ).text( "Please enter your email" );
}
});
});
Use focus for entry guidance and blur for checks that should happen when the user leaves the 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.