Code has been added to clipboard!

jQuery .focus(): Trigger the focus Event or Add an Event Handler to It

Reading time 2 min
Published Jan 8, 2018
Updated Oct 10, 2019

jQuery focus: Main Tips

  • The jQuery .focus() method attaches an event handler to a focus event.
  • It can also trigger the focus event.
  • The jQuery .focus() method can only be used on form elements and links. In the newest browser versions, you can change it by setting the tabindex property to any element.

Usage of .focus()

A focus event refers to an element receiving focus after being selected by clicking with the mouse or navigating to it (e.g., by tabbing). The focused element is usually highlighted, and any events related to the keyboard will affect it first as well.

The jQuery .focus() method can either trigger said event or attach a function to run when it is triggered. In the example below, a message shows as you select the input field by clicking inside it:

Example
$("input").focus(() => {
    $("span").css("display", "inline").fadeOut(3000);
});

Note: if you need to apply the handler to the children of the selected element as well, .focusIn() is a better choice that focus().

Two .focus() Syntax Types

To trigger the event, or make jQuery set focus, the syntax is simple – you don't need to add any parameters:

$("selector").focus()

To attach an event handler, you need to specify the selector and the function to run when focus event occurs:

$("selector").focus(function)

Note: you can only make jQuery set focus to visible elements. When hidden items use focus in jQuery, Internet Explorer reports an error.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX