[thelist] Re: Javascript: determining if a element has focus?

Danny Goodman dannyg at dannyg.com
Fri May 14 14:02:09 CDT 2004


on 5/14/04 2:04 AM, thelist-request at lists.evolt.org wrote:

> So I want to check if the focus is in either the username or password fields,
> and if 
> it is, I won't set the focus to the username field.
> 
> All that being said - anyway I can check if a field has focus, short of using
> the 
> onFocus and onBlur events to set some variables on the page?

You can set a custom property of the password field upon focus, and bypass
the username focus if that property flag is set.  Here's a skeleton to work
from (watch for email word wrapping in the onload handler):

<body onload="if (!document.forms[0].pw.typing)
document.forms[0].un.focus()">
<form>
Username:<input type="text" name="un" /><br />
Password:<input type="text" name="pw" onfocus="this.typing=true" />
</form>
</body>

So, it's using onfocus, but not taking up global var space. Works in quick
tests of IE6 and Mozilla.

Danny
http://www.dannyg.com
JavaScript Bible, 5th Edition (2004)



More information about the thelist mailing list