473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 14953
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

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

Similar topics

4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
4
by: Paul Thompson | last post by:
How do I determine in JavaScript the name of the object that focus is on?
22
by: DJ WIce | last post by:
Hi, I'm looking for a script to get the hi-est z-index on a page. I want my javascript menu to be always on top of the page (moves along on top when you scroll down). Does anyone know how to...
7
by: Phl | last post by:
hi, I can use the following code to set focus to standard html elements but if I try to do the same for a custom element, it seems to complain that it does not support a focus method. Does...
3
by: Dai Ba Wong | last post by:
Hi: Currently I am having a problem with my webpage. My page consist of two frames, one consist of input text field and the other contains link for different pop-up windows. The problem...
9
by: s_m_b | last post by:
I'm trying to get an <a> element to gain the focus onload, but only get back 'has no properties'. Reading through this ng, its clear that unless the element is within a form, this doesn't happen,...
3
by: Praveen | last post by:
In IE a table element will receive focus when you either tab into it or when you click anywhere within the table. Mainly it fires the onfocus event. This doesn't happen in Mozilla (Firefox and...
3
by: Burak Kadirbeyoglu | last post by:
Dear Developers, I have a simple question. There's a textbox and an imagebutton on my page. When I focus on the textbox and hit enter, the imagebutton is being clicked. However, when I click...
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.