469,344 Members | 6,523 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

How to find out if an element has focus

Frinavale
9,735 Expert Mod 8TB
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
May 16 '07 #1
3 14795
Frinavale
9,735 Expert Mod 8TB
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
May 16 '07 #2
mrhoo
428 256MB
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.
May 16 '07 #3
Frinavale
9,735 Expert Mod 8TB
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
May 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Paul Thompson | last post: by
22 posts views Thread by DJ WIce | last post: by
9 posts views Thread by s_m_b | last post: by
3 posts views Thread by Praveen | last post: by
3 posts views Thread by Burak Kadirbeyoglu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.