Search code examples
javascriptjquerybackbone.jsbackbone-eventsonfocus

Checking how an element gained focus


Context

I have a backbone app with an event listener for focus events on a textarea. Backbone uses jQuery events, so core of my question centers around jQuery focus events.

Question

Is there a way to tell how an element came into focus, be it by click or tab?

The behavior of how the cursor gets positioned needs to be handled differently between these two cases, however there doesn't seem to be a way to distinguish between the two offhand.

I could listen to click events, however will still need to listen to focus to capture tabbing - this will overlap with click event as it will also focus the textarea resulting in double events.

I may to rethink this entirely.

JSBin Example

$('textarea')
  .focus(function(event){
    console.log('You focused me by' + event.type);
  // Here I wish I know if the focus came from a 'click' or 'tab' events
  });
<!DOCTYPE html>
<html>
<head>
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
    <form>
        <input placeholder="focus me, then tab" type="text"><br>
        <textarea>Focus me via click.   Now try via tabbing.</textarea> 
    </form>
</body>
</html>


Solution

  • .onfocus() listener can get called in a number of ways which makes it a tricky even to bind to.

    • Click on element
    • Tab or Shift-Tab to element
    • jQuery programatic focus $( "#target" ).focus();
    • Switching between programs, and refocusing the internet browser

    There is no unique identifier in the onfocus event to determine how it came into focus.

    From what I found it is best to be more explicit and listen to click() and onkeyup() events to handle unique behaviors between them and avoid unexpected function calls (like the browser is refocused).

    onkeyup() is great for capturing tab events as the tab key will be released 'up' when tabbing in, but not when tabbing out.

    JSBin

    $('textarea')
     .click(focusedBy)
     .keyup(checkTab);
    
    function checkTab(event){
     if (event.keyCode === 9) {
      focusedBy(event);
     }
    }
    
    function focusedBy (event){
     console.log('You focused me by ' + event.type);
    }