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?
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.
In jQuery by using blur() property we can remove focus from input textbox field.
onblur Event: This event happens when an element is going to lose focus.
The blur() method removes focus from an element.
focusout
was added in v1.4. Three thoughts:
textbox
?$
)? 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With