Jquery - Set focus to window

11,947

Solution 1

I think what you're trying to do is to remove focus from the text box when you hit escape, so try this (in your event handler):

$(e.target).blur();

In this example focus never leaves from the window so you can't assign it back.

You could test to see if the target is valid for a blur call too - e.g. test if it's an input.

Solution 2

Yes, you don't need to give the function a name. There you are defining a function, not calling one:

Instead of:

$(document).ready(function ubsrt()
{

Use:

$(function ()
{
Share:
11,947
Peter
Author by

Peter

Updated on June 05, 2022

Comments

  • Peter
    Peter almost 2 years

    any ideas why this not work?

    $(document).ready(function ubsrt()
    {
        $(window).bind('keyup', function(e) { if (e.keyCode == '27') 
        { 
            $('body').append('focus window <br />');
            $(window).focus(); 
            $(document).focus(); 
        } });
    
       $('#test').focus();
    
    });
    

    example
    http://jsbin.com/agayen/edit#preview