473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find out if an element has focus

Frinavale
9,735 Recognized Expert Moderator Expert
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 14962
Frinavale
9,735 Recognized Expert Moderator Expert
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 Contributor
IE has a document.active Element 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 Recognized Expert Moderator Expert
IE has a document.active Element 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
17243
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 do myDomElement.click and the mouse doesn't matter, but in the case of an input image element, what happens is the submitted url has something like...
4
10424
by: Paul Thompson | last post by:
How do I determine in JavaScript the name of the object that focus is on?
22
10337
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 scann all items/elements on the page for a z-index? Thanks, Wouter
7
9929
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 anyone know what is the problem, if there is a work around? thank you
3
2614
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 follows: 1. At the beginning, focus is placed on an input text field of the first frame (so there's a blinking cursor on such field). 2. Then users...
9
9494
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, but according to the w3 TR on html 4.01, the a element can take onFocus commands, which to me means you can do...
3
7889
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 Netscape). Is there any setting or anyother way to force the table element to fire the onfocus and onblur events? Thanks
3
4412
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 somewher else on the page and hit enter, the imagebutton gets clicked again. How can I unfocus the imagebutton? The script I'm implementing is given...
2
3405
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 exist. (read the psedo code and comment if you need to know what it does) disregard the debugging , commented alerts. What i'm trying to do is...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.