Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery function when a textbox loses focus

Tags:

I have textbox that I want to run some jquery when the textbox loses focus, so after the user clicks out of the text box.

I have tried to do this

$("#textbox").focusout(function () {     alert("hello");  }); 

but I get an error saying Object doesn't support this property or method.

How can I do this then?

like image 217
twal Avatar asked Jun 30 '10 16:06

twal


People also ask

What is the difference between jQuery Focusout () and Blur () events?

The focusout event fires when an element is about to lose focus. The main difference between this event and blur is that focusout bubbles while blur does not.

How Stop focus in jQuery?

In jQuery by using blur() property we can remove focus from input textbox field.

Which event executed when an element loses focus?

onblur Event: This event happens when an element is going to lose focus.

How do you remove input field focus?

The blur() method removes focus from an element.


2 Answers

focusout was added in v1.4. Three thoughts:

  1. Could you be using an earlier version of jQuery?
  2. Does your field really have the id textbox?
  3. Are you also using Prototype or MooTools (or anything else that might be taking over $)? If so, use jQuery's noConflict mode and use jQuery instead of $.

Other than that, it should (does) work.

Here's an example (using an alert as you did): http://jsfiddle.net/QzmZp/1/
and another not using an alert (because that freaked IE7 out): http://jsfiddle.net/QzmZp/2/
Someone earlier asked about browser versions, I've tried the above with Chrome 5, IE6, IE7, and FF3.6; all fine.

I did both an input and a textarea because I wasn't sure which you were using.

like image 122
T.J. Crowder Avatar answered Oct 20 '22 22:10

T.J. Crowder


jQuery("#textbox").blur(function() {   alert("hello"); }); 

blur is the event that fires when an element loses focus. Check out jQuery.blur.

EDIT

Not sure if this is what you want, but if you are really trying to use focusout check out T. J. Crowder's solution. For your situation though, the blur event might be want you need since you want to detect the loss-of-focus on the textbox itself. focusout fires when an element or any element inside that element loses focus.

like image 21
Vivin Paliath Avatar answered Oct 20 '22 22:10

Vivin Paliath