KVISH
KVISH

Reputation: 13208

jquery + lose focus of a textbox

I'm completely stuck.. I tried a few things. I want to lose the focus of a textbox when a user clicks on it.

I tried this:

$('.tagit-input[type=text]').focus(function(){
    $('.tagit-input[type=text]').blur();
});

as well as this, where I am giving the focus to a different text box instead:

$('.tagit-input[type=text]').focus(function(){
    $('.tagit[type=text]').focus();
});

as well as various methods in keydown etc. Is there something I'm missing? I'm just trying to lose focus of one box with a class .tagit-input

Upvotes: 0

Views: 3048

Answers (2)

Vinayak Phal
Vinayak Phal

Reputation: 8919

Try .focusout http://api.jquery.com/focusout/

Upvotes: 1

Eli
Eli

Reputation: 572

If your only desire is to lose the focus of the element when focused-on, you can just trigger the .blur() DOM event as the handler when the .focus() event is called or to select another element to focus, do the same and in addition, just trigger a focus on the desired element.


In your case all you would need to do to lose focus and select another element would be:

 $('.tagit-input[type=text]').focus(function(){
        $('.not-tagit-input[type=text]').focus();
    });

Here is a working example.

Upvotes: 0

Related Questions