Page 1 of 1

Check if text field is active

Posted: Sun Jul 04, 2004 2:37 pm
by MarK (CZ)
Hi all!

I'm trying to find something to check that a text field is selected (focused). I need some action to be done only if the field is currently active.

Thx in advance. :)

Posted: Sun Jul 04, 2004 2:48 pm
by feyd
onfocus()

i.e.

Code: Select all

<script language="Javascript">
function foc(obj)
&#123;
   alert(obj.name);
&#125;
</script>

......

<input type="text" name="whatever" onfocus="foc(this)" />

Posted: Sun Jul 04, 2004 2:51 pm
by MarK (CZ)
Yeah, I could maybe use that too, thx...

But is there an element option that can tell me whether is the object active?

Posted: Sun Jul 04, 2004 3:11 pm
by feyd
What are you trying to do?

Posted: Sun Jul 04, 2004 3:20 pm
by MarK (CZ)
I'm just catching the onKeyPress event with a function that does some things. And I want it to do some of them only when one text field is selected. And that doesn't have to be directly after the user's focus on the element. So I need something to put in the if clause to check whether is the INPUT element active.

Code: Select all

document.onkeypress = KeyPressed;

Posted: Sun Jul 04, 2004 3:29 pm
by feyd

Code: Select all

<input type="text" name="whatever" onkeypress="specialstuff(this)" />
you could use the onfocus() to switch modes for the keypress handler too.. You may want to switch modes back, when you get onblur()...

Posted: Sun Jul 04, 2004 4:59 pm
by MarK (CZ)
feyd wrote:

Code: Select all

<input type="text" name="whatever" onkeypress="specialstuff(this)" />
you could use the onfocus() to switch modes for the keypress handler too.. You may want to switch modes back, when you get onblur()...
Yeah, I know that, I was just wondering if there exists a property to say whether is the element active.

Thx for the replies