Click to See Complete Forum and Search --> : Focused text field


MarkE
02-26-2003, 05:33 PM
How can I tell if a text field in a form has focus? Is it something like :

<script>
if (document.<textfield name here>.focus){alert(true)} else {alert(false)}
</script>

? I need some kind of "if(){}" test to check for focus or no focus.

gil davis
02-26-2003, 05:50 PM
You'd have to make one.

<input ... onfocus="this.focus=true" onblur="this.focus=false">

MarkE
02-26-2003, 05:54 PM
I don't know of any attribute called "focus", but I'll give it a shot.

gil davis
02-27-2003, 05:47 AM
Originally posted by MarkE
I don't know of any attribute called "focus"There isn't one until you run that code. You have to be careful that you do not execute any script that accesses the new attribute until after onload.

Also, Dave's caution is very appropriate. To make it more clear, perhaps you could call it "myFocus".

You only have onfocus and onblur event handlers to work with, but by creating a "static" representation of the "current" status, you can get what you want.