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!


DavidWareAsked:
Who is Participating?
 
yas022100Commented:
What you can also do is instead of trapping what user did, change action depending on what user clicked
something like

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Form Name=frmInput ID=frmInput Action=Change.htm Method=post>
<table>
<tr>
<td><input type="submit" Name="ActEdi" Value="  <<  " onClick=First()></td>
<td><input type="submit" Name="ActEdi" Value="  <  " onClick=Back()></td>
<td><input type="submit" Name="ActEdi" Value="  >  " onClick=Forward()></td>
<td><input type="submit" Name="ActEdi" Value="  >>  " onClick=Last()></td>
<td><input type="submit" Name="ActEdi" Value="  Add  " onClick=Add()></td>
<td><input type="submit" Name="ActEdi" Value="Delete" onClick=Delete()></td>
<td><input type="submit" Name="ActEdi" Value="Search" onClick=Search()></td>
</tr>
</table>
</Form>

<Script Language=Javascript>
   function First()
   {
      document.frmInput.action = 'Forward.htm'
   }
   function Back()
   {
      document.frmInput.action = 'back.htm'
   }
   function Forward()
   {
      document.frmInput.action = 'Forward.htm'
   }
   function Last()
   {
      document.frmInput.action = 'Last.htm'
   }
   function Add()
   {
      document.frmInput.action = 'Add.htm'
   }
   function Delete()
   {
      document.frmInput.action = 'Delete.htm'
   }
   function Search()
   {
      document.frmInput.action = 'Search.htm'
   }
</Script>
</BODY>
</HTML>

Does that make sense?
0
 
edemcsCommented:
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?
0
 
DavidWareAuthor Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
avnerCommented:
<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>
0
 
bebonhamCommented:
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>
0
 
bebonhamCommented:
just try it
0
 
DavidWareAuthor Commented:
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.
0
 
avnerCommented:
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.
0
 
bebonhamCommented:
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



0
 
DMNCommented:
Did you try to give different names to your buttons? focus() should work then.
0
 
DavidWareAuthor Commented:
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.
0
 
DavidWareAuthor Commented:
Thanks to all for your input.
David
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.