I need to determine what caused a focus event.
Ideally, I want to differentiate between a click, a tab/keyboard input, and a manual (via code) trigger.
How can I do this?
I’m looking at the event object, but I’m not seeing anything too useful.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the focus comes from a
$x.focus()call, then the event won’t have anoriginalEventproperty because there was no event from the browser so:To differentiate between keyboard and mouse based focus events, you could try binding a
keydownhandler to everything else to detect a Tab or Shift-Tab but that would be a gross hack and probably not reliable; for example, on an iPad, you don’t hit Tab to move to the next field, you hit Next or Previous in the popup keyboard to move around and those may not register as key presses at all.There’s a similar question about
clickevents that might be of interest as well:As you note in the comments, you could trap
clickevents to detect a mouse-based focus change and set a flag somewhere to remember it. Then you’d have this:originalEventin the jQuery event then the focus change was triggered manually (i.e.$x.focus()or similar).You’d have to be careful that your click and focus events came in the right order and you’d need to make sure the flag was cleared when you’re done with it. This might not be bullet proof but maybe it doesn’t need to be.