Link to home
Start Free TrialLog in
Avatar of DavidWare
DavidWareFlag for United States of America

asked on

Send Focus( ) to a button & Click it

I have 7 buttons on an HTML form for various record navigation and functions (FIRST, PREV, NEXT, LAST, ADD, DELETE, SEARCH)

<table>
<tr>
<td><input type="submit" Name="ActEdi" Value="  <<  "></td>
<td><input type="submit" Name="ActEdi" Value="  <  "></td>
<td><input type="submit" Name="ActEdi" Value="  >  "></td>
<td><input type="submit" Name="ActEdi" Value="  >>  "></td>
<td><input type="submit" Name="ActEdi" Value="  Add  "></td>
<td><input type="submit" Name="ActEdi" Value="Delete"></td>
<td><input type="submit" Name="ActEdi" Value="Search"></td>
</tr>
</table>

I can set the focus to a particular INPUT box ...
{
document.forms[0].MyInput.focus();
}

but this doesn't work for a particular SUBMIT button.

I have figured out the JavaScript to trap FunctionKeys, and want to set focus and activate a button based on the FunctionKey pressed.

The F2 button should "Click" the second, or "Previous" (  <  ) button...

<td><input type="submit" Name="ActEdi" Value="  <  "></td>

Can anyone help me do that?  

I don't need the KeyTrap code, just what would go in the F2( ) function.

TIA!


Avatar of edemcs
edemcs
Flag of United States of America image

if you can't find the focus function for the buttons, do an onKeyPress:

if (buttonPressed == "<")
 {
  document.location "www.somewhere.com";
 }
else if(buttonPressed ==">")
.
.
.
end if

does that make sense?
Avatar of DavidWare

ASKER

edemcs,
I don't understand what you mean by "if you can't find the focus function for the buttons..."

What I'm trying to do is get the F2 Key to SUBMIT to the new asp file with the value "  <  ".  

You see, the form POSTs to another asp file,

          <Form Method=Post Action="HCFRCoUpdMov.asp" id=form1 name=form1>

passing the VALUE of the "ActEdi" button that was clicked.  

          <input type="submit" Name="ActEdi" Value="  <  ">

So if the user clicks the second button (with a value of "  <  ") then the new asp page interprets that for a particular function.

So what I'm trying to do is get the F2 Key to SUBMIT to the new asp file with the value "  <  ".  The way I was designing was to have the button get "clicked" when the F2 Key is pressed, but I don't know how to set the focus to the button, and then "click" it.
Avatar of avner
avner

<BODY onload="document.bb.aa.click()">
<form action="http://www.yahoo.com" id="bb" name="bb">
<input type="submit" id="aa" name="aa">
</form>
</BODY>
when you

<HTML><HEAD>

<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">

function al()
{
alert('kp')
document.forms[0].but.focus()
document.forms[0].but.onclick()
}
</SCRIPT>

</HEAD>


<BODY>
<a href="#" onclick="al();return false">click to give focus and run the function in the buttons</A>
<FORM> <INPUT TYPE="BUTTON" ONCLICK="alert('click')" NAME="but"></FORM></BODY></HTML>
just try it
avner & bebonham,
Thanks to both of you for your input.

I think the real problem is that all of my buttons have the same name.  Even though this works for Clicking, it kind of screws up setting focus.

I'm trying to do this in KeyCapture, but both of you had me place something on the form to click, and even though bebonham added ...

document.forms[0].but.focus()
document.forms[0].but.onclick()

... there was only one button named "but".  So the Focus( ) worked fine.

I will keep working on it.
1. you can add an ID to all of the buttons , this way when you send it , it'll use the name , but using DHTML you can use the ID.

another approach is an IE only :

document.getElementsByName("BUT")[2].click()

will click on the thrid button.
ASKER CERTIFIED SOLUTION
Avatar of yas022100
yas022100

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this works for me on ie ns4+ ns6


<HTML><HEAD>

<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">

function al()
{
document.forms[0].but[1].focus()
document.forms[0].but[1].click()
}
</SCRIPT>

</HEAD>


<BODY>
<a href="#" onclick="al();return false">click to give focus and run the function in the buttons</A>
<FORM> <INPUT TYPE="BUTTON" ONCLICK="alert('click1')" NAME="but" VALUE="but1">
 <INPUT TYPE="BUTTON" ONCLICK="alert('click2')" NAME="but" VALUE="but2">
  <INPUT TYPE="BUTTON" ONCLICK="alert('click3')" NAME="but" VALUE="but3">
   <INPUT TYPE="BUTTON" ONCLICK="alert('click4')" NAME="but" VALUE="but4">


</FORM></BODY></HTML>



Bob

thanks to avner: onclick doesn't work in ns6



Did you try to give different names to your buttons? focus() should work then.
sorry for the delay.  the customer backed out of this project and we haven't had time to finish it unpaid, but I will come back here to clean this up soon.
thanks for your patience.  I won't disappear.
Thanks to all for your input.
David