{"name":"focus","type":"method","title":".focus()","deprecated":null,"removed":null,"desc":"Bind an event handler to the \"focus\" JavaScript event, or trigger that event on an element.","categories":["events/form-events","forms","version/1.0","version/1.4.3"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":{"desc":"A function to execute each time the event is triggered.","name":"handler(eventObject)","type":"Function"}},{"added":"1.4.3","argument":[{"desc":"An object containing data that will be passed to the event handler.","name":"eventData","type":"Object","optional":"true"},{"desc":"A function to execute each time the event is triggered.","name":"handler(eventObject)","type":"Function"}]},{"added":"1.0"}],"examples":[{"desc":"Fire focus.","css":"\n span {\n display: none;\n }\n","code":"\n$( \"input\" ).focus(function() {\n $( this ).next( \"span\" ).css( \"display\", \"inline\" ).fadeOut( 1000 );\n});\n","html":"\n

focus fire

\n

focus fire

\n"},{"desc":"To stop people from writing in text input boxes, try:","code":"\n$( \"input[type=text]\" ).focus(function() {\n $( this ).blur();\n});\n"},{"desc":"To focus on a login input box with id 'login' on page startup, try:","code":"\n$( document ).ready(function() {\n $( \"#login\" ).focus();\n});\n"}],"longdesc":"\n \n
\n

Attempting to set focus to a hidden element causes an error in Internet Explorer. Take care to only use .focus() on elements that are visible. To run an element's focus event handlers without setting focus to the element, use .triggerHandler( \"focus\" ) instead of .focus().

\n
\n

For example, consider the HTML:

\n
\n<form>\n  <input id=\"target\" type=\"text\" value=\"Field 1\">\n  <input type=\"text\" value=\"Field 2\">\n</form>\n<div id=\"other\">\n  Trigger the handler\n</div>\n    
\n

The event handler can be bound to the first input field:

\n
\n$( \"#target\" ).focus(function() {\n  alert( \"Handler for .focus() called.\" );\n});\n    
\n

Now clicking on the first field, or tabbing to it from another field, displays the alert:

\n

\n Handler for .focus() called.\n

\n

We can trigger the event when another element is clicked:

\n
\n$( \"#other\" ).click(function() {\n  $( \"#target\" ).focus();\n});\n    
\n

After this code executes, clicks on Trigger the handler will also alert the message.

\n

The focus event does not bubble in Internet Explorer. Therefore, scripts that rely on event delegation with the focus event will not work consistently across browsers. As of version 1.4.2, however, jQuery works around this limitation by mapping focus to the focusin event in its event delegation methods, .live() and .delegate().

\n "}]}