How an element got focus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alejandro
    New Member
    • Mar 2007
    • 27

    How an element got focus

    Is there an easy way to know exactly how an element got focus in it's onfocus function? (was the element granted focus because of a mouse click?, because the user tabbed into it?, because it's focus() function was called?).
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    function(e){
    e=e||window.eve nt;
    alert(e.type);
    }

    Comment

    • alejandro
      New Member
      • Mar 2007
      • 27

      #3
      wouldn't work, in the onfocus function, the event type will always be focus (already tested, just in case).

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        If you have a single handler that is called onmouseover and on focus,
        and on click and keypress, you can examine the event type to
        make some decision about execution.
        If you set a handler only to a focus event, focus is all you get.

        You could set up a global variable to hold the last event,
        and compare it to the current event,
        but you'd have to write an assignment in every handler (except focus)
        to update it.

        Comment

        • alejandro
          New Member
          • Mar 2007
          • 27

          #5
          yes, you are correct.
          unfortunately working on a huge project, where I just can't change the way events are handled (1.200+ files with <input onfocus="focusE vent();" /> all everywhere), but fortunately, I was able to work around the issue I had in a completely different manner :)
          thank you very much for your help :)

          Comment

          Working...