On Jan 9, 11:25 pm, Gordon <gordon.mc...@ntlworld.com> wrote:
> I've got a field on a web page that needs some javascript behaviour
> when it gains or loses focus, so I attached the appropriate handlers
> in a $(document).ready to the field.
>
> It all works fine, except if the user focuses the input field before
> the ready event fires.  In this case the behaviour doesn't occur.

That is a major issue with dynamically attached listeners.  The longer
the page takes to load, the more likely it is that it will occur.

>
> The old version used onfocus and onblur attributes in the tag itself,
> which is obviously a very bad way of doing it,

Obviously?  There is nothing wrong with inline listeners.  Ultimately,
you are trying to create a document with appropriate elements and
functionality - how you go about that should be determined by your
functional requirements.

If you have a requirement for elements to have listeners from the
instant they appear, and there is a good chance that dynamic listeners
wont be attached quickly enough, put them inline.


> but did have the
> advantage that the behaviour would occur as soon as the input field
> became visible.

Which seems to be what you want, so why is it so bad?

>
> Is there a friendlier way of attaching behaviour before the ready
> event?

An alternative is to use a script element immediately after the
element you want the listener attached to, but I'll guess that you
like that approach even less.


--
Rob

Reply via email to