Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you tell when a form element has focus?????

Status
Not open for further replies.

villan

Programmer
Jul 5, 2001
6
GB
I have an ASP with a form containing 10 buttons, in a "keypress" function i need to know which button currently has focus, I've tried the following - but they don't work:

if (document.form1.button1.focus() == true)

and

if (document.form1.button1.focus())

how do i confirm which item has currenly got focus?
 
hie
why do u want 2 know that? isnt it much simplier 2 use button names & onclick handlers? regards, vic
 
I am designing an intranet site for one of my customers & i want to write some code that acts as follows:

when the user presses the "up" arrow, the focus moves to the button above & vice versa on "down" arrow.

I decided that if i could identify which button had focus at the time the up arrow was pressed (in the keydown function) i could set the focus on the previous element (button).

The resulting effect is that of:
pressing the down key instead of the TAB key
pressing the up key instead of the SHIFT & TAB keys.

Overall i want the site to look & act like an in-house application rather than a web site, so they can use the keyboard to navigate.
 
I think your best option would be to use the onFocus event handler to set a variable based on the current position.

<INPUT TYPE=&quot;button&quot; VALUE=&quot;Move Next&quot; onFocus=setPos(1)>

and then have a variable defined globally to hold the position.

<SCRIPT>
var currPos;

function setPos(sp){currPos = sp;}
</SCRIPT>
then if you use an array to hold the names of the buttons you can set the focus in your keypress event handler.

Hope this helps
RnK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top