Forum Moderators: open

Message Too Old, No Replies

Getting the current element which has focus

Getting the current element which has focus

         

sireesha

11:47 am on Jan 16, 2004 (gmt 0)

10+ Year Member



Hi all,
Can I get the current element in my document which has the focus? Basically I want a method which returns me true if the focus is set on a particular element and returns me false if the focus is not on that element. Is there any inbuilt function for this in Javascript? If yes,whatz it and if no, how do I achieve my objective?Any help regarding will be really helpful...

Thanks,
Sireesha

Purple Martin

1:05 am on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use the onFocus event handler. For example, in the head you could declare a variable...

var currentElement = ""

...and then you could use the onFocus event of every element on your page...

onFocus(currentElement=this.id)

...to set the value of currentElement to the id of the element that has the focus.

I haven't tested this, it's just an idea. Hope it works!

Joshie76returns

8:56 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Just for reference, in IE (4.0+) you can use the activeElement property of the document object:

var currentElement = document.activeElement

[msdn.microsoft.com...]

As far as I'm aware this is IE only though. :(