Skip to main content

There are two important jQuery functions for dealing with element's focus. They are:

.focus(handler) - to set focus to a field
.blur(handler) - to get focus from a field

- Advertisement -
- Advertisement -

A part of HTML page:

Test field #1: <input id="testfield1" type="text" />
Test field #2: <input id="testfield2" type="text" />

jQuery set focus to a field

$(document).ready(function() {
    $('#testfield1').focus(function(){
        alert('Test Field 1 has focus.');
    });
});

jQuery get focus from a field

$(document).ready(function() {
    $('#testfield1').blur(function(){
        alert('Test Field 1 lost focus.');
    });
});

- Advertisement -