Friend
Friend

Reputation: 1346

Jquery focus out after pressing enter key

My code is below:

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).focusout();

    }
});

As you can see on the code above, When a user presses the enter key first callajax() is run(working fine). After that I want to focus out from the .summaryT input box, How can I achieve this?

Upvotes: 10

Views: 18423

Answers (4)

ashish patel
ashish patel

Reputation: 179

Here is the solution even if Mousetrap is activated

    $('.selectorClass').keypress(function(e)
    {
        if(e.which == 13)
        {
            // 13 is a code to hit keyboard enter button
            alert('Enter is pressed');
        }
    });

    $('#selectorID').keypress(function(e)
    {
        if(e.which == 13)
        {
            // 13 is a code to hit keyboard enter button
            alert('Enter is pressed');
        }
    });

Upvotes: 0

Liam Allan
Liam Allan

Reputation: 1115

use the jquery blur() event

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).blur();

    }
});

Upvotes: 1

Bibhu
Bibhu

Reputation: 4081

Try this

$('.summaryT').keypress(function(e){
    if(e.which == 13){
        callajax();
        $(this).blur();    
    }
});

Upvotes: 16

fabrik
fabrik

Reputation: 14365

Since AJAX stands for asynchronous, you may want to call focusout() after the call successfully finished.

Upvotes: 1

Related Questions