473,379 Members | 1,323 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,379 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 14951
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.