Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a user unfocuses on an input box using jQuery?

Is it possible to detect when the user unfocuses an input box using jquery? I.e when they click off of it an event fires.

like image 776
user1202130 Avatar asked Jan 17 '23 12:01

user1202130


2 Answers

I believe the blur function is what you are looking for.

$( "input[type=text]" ).blur( function() {
    // unfocus event
});
like image 183
Will Avatar answered Jan 31 '23 08:01

Will


The blur event will fire any time an element loses focus.

If you are trying to determine when any input element on the page loses focus, then use input as your selector.

$("input").blur(function() {
    //This event fires every time any input element on the page loses focus
});

If you are only trying to determine when a particular input element on the page loses focus, then using the element's id is the most efficient jQuery selector.

$("#exampleID").blur(function() {
    //This event fires if the element with an id of "exampleID" loses focus
});
like image 31
Greg Franko Avatar answered Jan 31 '23 09:01

Greg Franko