Problem tracking focus of text input elements (Mozilla?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Wright

    #1

    Problem tracking focus of text input elements (Mozilla?)


    Hi all.

    Hopefully this should demonstrate the problem I'm having:



    (I'm testing it in Mozilla only, but I'm not sure if it's actually a
    Mozilla-only problem)

    I'm capturing the focus and blur events for the document, updating a
    global Javascript variable to keep track of the "currently focussed"
    element.

    The problem is that it just doesn't seem to work for text (or
    textarea) input elements. Just about anything else, no problem, but
    not text or textarea.

    You can see by switching focus (eg. by tabbing) through the five input
    elements. Entering/leaving the button, checkbox or select elements
    generates a focus/blur event. Entering/leaving the text/textarea
    elements generates absolutely nothing.

    Can anyone suggest what's wrong with this picture? :) Is it a browser
    bug or am I fundamentally misunderstandin g something about tracking
    element focus?


    Any help greatly appreciated. :)

    Pete.

    PS. I'd just like to mention that tracking focus bugs is hell when
    just about everything you do changes the focus. :)
    --

    "Reader, beware: This section is highly mathematical. Well, maybe not
    _highly_ mathematical, but it's got a bunch of symbols and scary-looking
    formulas. You have been warned." -- sci.crypt FAQ
  • Martin Honnen

    #2
    Re: Problem tracking focus of text input elements (Mozilla?)



    Peter Wright wrote:

    [color=blue]
    > Hopefully this should demonstrate the problem I'm having:
    >
    > http://flooble.net/~pete/focus-problem-demo/
    >
    > (I'm testing it in Mozilla only, but I'm not sure if it's actually a
    > Mozilla-only problem)
    >
    > I'm capturing the focus and blur events for the document, updating a
    > global Javascript variable to keep track of the "currently focussed"
    > element.
    >
    > The problem is that it just doesn't seem to work for text (or
    > textarea) input elements. Just about anything else, no problem, but
    > not text or textarea.[/color]

    If you look at the W3C DOM Events specification here
    <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-eventgroupings-htmlevents>
    then it says about the focus event:

    focus
    The focus event occurs when an element receives focus either via a
    pointing device or by tabbing navigation. This event is valid for the
    following elements: LABEL, INPUT, SELECT, TEXTAREA, and BUTTON.

    * Bubbles: No
    * Cancelable: No
    * Context Info: None

    so the focus event is not supposed to bubble up the DOM tree and
    therefore if you only look for focus events at the document or window
    level then you are not supposed to see anything if focus is set on an
    input or a textarea or a button. The event is fired on the target and
    that is all that is supposed to happen. As far as I test here the focus
    event is properly fired on the controls, it just doesn't bubble which it
    is not supposed to do.
    So the bug in Mozilla is more likely that for some controls the focus
    event bubbles while it shouldn't. There are open bugs on that:
    <https://bugzilla.mozill a.org/buglist.cgi?que ry_format=advan ced&short_desc_ type=allwordssu bstr&short_desc =focus+event+bu bbl&product=Cor e&component=DOM &component=DOM+ to+Text+Convers ion&component=D OM%3A+Abstract+ Schemas&compone nt=DOM%3A+Core& component=DOM%3 A+CSSOM&compone nt=DOM%3A+Event s&component=DOM %3A+HTML&compon ent=DOM%3A+Leve l+0&component=D OM%3A+Load+and+ Save&component= DOM%3A+Mozilla+ Extensions&comp onent=DOM%3A+Tr aversal-Range&component =DOM%3A+Validat ion&component=D OM%3A+Views+and +Formatting&com ponent=Event+Ha ndling&long_des c_type=substrin g&long_desc=&bu g_file_loc_type =allwordssubstr &bug_file_loc=& status_whiteboa rd_type=allword ssubstr&status_ whiteboard=&key words_type=allw ords&keywords=& emailassigned_t o1=1&emailtype1 =exact&email1=& emailassigned_t o2=1&emailrepor ter2=1&emailqa_ contact2=1&emai ltype2=exact&em ail2=&bugidtype =include&bug_id =&votes=&chfiel dfrom=&chfieldt o=Now&chfieldva lue=&cmdtype=do it&order=Reuse+ same+sort+as+la st+time&field0-0-0=noop&type0-0-0=noop&value0-0-0=>






    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Peter Wright

      #3
      Re: Problem tracking focus of text input elements (Mozilla?)

      Martin Honnen <mahotrash@yaho o.de> wrote:[color=blue]
      > Peter Wright wrote:[color=green]
      > > http://flooble.net/~pete/focus-problem-demo/
      > >
      > > (I'm testing it in Mozilla only, but I'm not sure if it's actually a
      > > Mozilla-only problem)[/color][/color]
      [ snip ][color=blue]
      > If you look at the W3C DOM Events specification here
      > <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-eventgroupings-htmlevents>
      > then it says about the focus event:
      >
      > focus
      > The focus event occurs when an element receives focus either
      > via a pointing device or by tabbing navigation. This event is valid
      > for the following elements: LABEL, INPUT, SELECT, TEXTAREA, and
      > BUTTON.
      >
      > * Bubbles: No[/color]

      Ahaaaaaa...

      I should really check the official DOM documentation more often.
      [color=blue]
      > * Cancelable: No
      > * Context Info: None
      >
      > so the focus event is not supposed to bubble up the DOM tree and
      > therefore if you only look for focus events at the document or window
      > level then you are not supposed to see anything if focus is set on an
      > input or a textarea or a button.[/color]

      Okay, that does help to explain things... I was going nuts with this,
      assuming that it was _supposed_ to bubble. Dammit.
      [color=blue]
      > The event is fired on the target and that is all that is supposed to
      > happen. As far as I test here the focus event is properly fired on
      > the controls, it just doesn't bubble which it is not supposed to do.[/color]

      Argh. *sigh*

      I was hoping to use this technique to track the current focus - I
      guess it'll only work (for some value of "work") if I specifically set
      the "focus" event handler to every single element in the display.

      Ah well, I guess that'll be an acceptable solution. Sort of. :)
      [color=blue]
      > So the bug in Mozilla is more likely that for some controls the focus
      > event bubbles while it shouldn't. There are open bugs on that:[/color]
      [ snip open bugs ]

      Thanks for your help Martin, I appreciate it.
      [color=blue]
      > Martin Honnen
      > http://JavaScript.FAQTs.com/[/color]

      Pete.
      --

      "What is a 'broken killfile'? One that only wounds messages rather
      than killing them?" -- Craig Dickson

      Comment

      Working...