views:

51

answers:

2

using Jquery/javascript

Is it: $(document).focus()?

A: 

$().method() fires the listeners associated with that method. If you need to call functions on dom elements, index the jQuery object.

i.e.

$(document)[0].focus()

Although why you wouldn't just use document.focus is probably a question worth answering.

Stefan Kendall
+2  A: 

The .focus() method on a jQuery-wrapped element will bind to the onfocus event if a function is passed as the first parameter, or trigger attached event handlers with no parameter.

I'm not sure why you're trying to do what you're trying to do, but if you're trying to blur the currently active element, no matter what element it is, you can use:

if ("activeElement" in document)
    document.activeElement.blur();

This isn't advised though, as doing so will reset the tabbing order and potentially annoy keyboard-navigating users.

Andy E
Best put a check in like `if ('activeElement' in document)` first, as it's not supported in all browsers.
bobince
@bobince: Added it as a precautionary measure. `activeElement` is supported in the latest versions of Firefox, Opera, WebKit and IEs 6 through 8 and is also part of the HTML5 spec, so hopefully it will be widely supported anyway.
Andy E