tags:

views:

40

answers:

2

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?

+1  A: 
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.

Vivin Paliath
blur worked for me thank you! Not sure why i get the error on focus out but this will work.Thanks!!!!
twal
`focusout` fires for the textbox as well, it *also* bubbles though.
Nick Craver
+2  A: 

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.

T.J. Crowder
i just double checked. it is 1.4.2
twal
double checked again my field has an id of textbox i copied and pasted it to the selector just to be sure. and still the same.Is it does it both on IE and FF browser doesn't effect it.
twal
@twal - Do you have another library on the page, prototype for example?
Nick Craver
@Nick: LOL, jinx! ;-)
T.J. Crowder
nope I don't have any other libary. I just re-down loaded jquery 1.4.2 and replaced the existing with the new and and voila focusout worked. Weird.. whatever, Thank you guys for all your help!!!
twal
@twal: With respect, why accept an answer related to `blur` then? It seems to me neither answer solved your problem (though both tried to). Accepting an answer is for when the answer *solved* the problem; *upvoting* is for answers that helped, even if they didn't solve the problem. Neither of these did the former, though both did the latter.
T.J. Crowder
really? does it matter that much? There were only two answers. Blur did solve my problem which was to get the textbox to show up when the user clicked out of the text box. So I clicked it as the answer to mainly mark that my problem was solved.
twal
I do appreciate your help though please don't get me wrong. Thank you
twal
@Twal: Glad that helped. But I didn't mean you should accept *this* answer, it didn't actually solve the problem any more than the other one! :-) If you feel you need to accept one or the other, please do go with your original choice. And no, it doesn't matter that much, but the idea is that questions with *accepted* answers have actually been answered. What I'd say you should do is post your own answer saying exactly what you said (re-downloaded and it worked), then accept that (there'll be a two-day delay before you can do that).
T.J. Crowder
@twai what T. J. said :) If neither answer is the solution, simply upvote if they helped. What you can do is post your *own* solution as an answer and then accept that (when you are able to - usually in 2 days).
Vivin Paliath
gotcha, thanks guys. This is a good site. lots of people willing to help I like it
twal