Connecting Tech Pros Worldwide Forums | Help | Site Map

How to find out if an element has focus

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#1: May 16 '07
I've been searching for a little while now and can't figure out how to determine if a particular element has focus.

I'm changing either a text box representing the Hours in time or Minutes in time depending which text box currently has focus...

So basically I'm trying to change the value of a text field depending on weather or not it has focus...but for the life of me I can't figure out how to determine this.

My simple code so far:
Expand|Select|Wrap|Line Numbers
  1.   <script type='text/javascript'>
  2.  
  3.         function Up(MinTextBox, HrTextBox)
  4.          {
  5.  ----------->if(document.getElementById(MinTextBox).......has focus?
  6.              {          
  7.                  document.getElementById(MinTextBox).value = Number(document.getElementById(MinTextBox).value) +1
  8.               }
  9.               else
  10.               {
  11.                   document.getElementById(HrTextBox).value = Number(document.getElementById(HrTextBox).value) +1
  12.               }
  13.          }
  14.    </script>
  15.  
Thanks in advance!

-Frinny

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: May 16 '07

re: How to find out if an element has focus


Quote:

Originally Posted by Frinavale

I've been searching for a little while now and can't figure out how to determine if a particular element has focus.

I'm changing either a text box representing the Hours in time or Minutes in time depending which text box currently has focus...

So basically I'm trying to change the value of a text field depending on weather or not it has focus...but for the life of me I can't figure out how to determine this.

My simple code so far:

Expand|Select|Wrap|Line Numbers
  1.   <script type='text/javascript'>
  2.  
  3.         function Up(MinTextBox, HrTextBox)
  4.          {
  5.  ----------->if(document.getElementById(MinTextBox).......has focus?
  6.              {          
  7.                  document.getElementById(MinTextBox).value = Number(document.getElementById(MinTextBox).value) +1
  8.               }
  9.               else
  10.               {
  11.                   document.getElementById(HrTextBox).value = Number(document.getElementById(HrTextBox).value) +1
  12.               }
  13.          }
  14.    </script>
  15.  
Thanks in advance!

-Frinny


You know something I never thought of is when the button gets clicked it has focus. I guess I'm going to have to create a function for the text fields that sets a variable to the text field's name on the onFocus event.


-Frinny
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#3: May 16 '07

re: How to find out if an element has focus


IE has a document.activeElement property, but to make it compatible you'd have to define a global and write a new value from every element's focus event.

Much easier to call the function from both element's focus event, and query the event target (or srcElement) inside the function, to see who is calling.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#4: May 17 '07

re: How to find out if an element has focus


Quote:

Originally Posted by mrhoo

IE has a document.activeElement property, but to make it compatible you'd have to define a global and write a new value from every element's focus event.

Much easier to call the function from both element's focus event, and query the event target (or srcElement) inside the function, to see who is calling.


Thanks for your response MrHoo.

This is precisely how I solved the problem.

:)
-Frinny
Reply