Peter
Peter

Reputation: 11835

Jquery - Set focus to window

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

Upvotes: 4

Views: 12855

Answers (2)

Timbo
Timbo

Reputation: 4533

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.

Upvotes: 4

Alex
Alex

Reputation: 7374

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 ()
{

Upvotes: 1

Related Questions