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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set the default button to enter ? 3

Status
Not open for further replies.

need2progm

Programmer
Dec 13, 2002
92
0
0
US
I am having a brain fart.. I can not remember how to set a button as the default to the enter key. I thought it was setting the tab index to 0 and the other buttons to (1,2,3,4, ect).
 
I've gone around and around with that problem. Sometimes it works as described in various places on MS's website, sometimes it's just bunk.

Instead I made a JavaScript workaround and it ended up opening a whole new avenue of possibility.

Here's the code.
Code:
<!-- In the <Head> tag -->
<script lang=&quot;Javascript&quot;>
  function onkey() {
    if (window.event.keyCode==13) {
      document.all[&quot;buttonname&quot;].focus();
    }

  // You could even put in a &quot;Home&quot; key function!
    if (window.event.keyCode==36) {
      window.location=&quot;homepageurl&quot;;
    }
  }
</script>

Then the <body> tag looks like this...

Code:
<body onkeydown=&quot;onkey();&quot;>

Of course there's a million things you can do... use a &quot;switch&quot; block to make a whole keypad interface... etc.

If you have multiple possible default buttons (based on what control may have the focus at the time), then you'll want to add a <input id=&quot;a&quot; type=&quot;hidden&quot;> tag and then use the onfocus events of the text boxes to change the value of the hidden field. Then put an &quot;if&quot; inside the &quot;keycode is 13&quot; block to see which button to send the focus to before the enter key is processed.

Sorry for the long answer but I've gone around and around with this problem myself, and a lot of trial and error has come to this conclusion...

Hope this helps,
Elijah
 
Thanks for all you hard work!!!

This has really helped.

I see other possibilities though .. what about a back button so not use the browser back button.

where you have ... window.location=&quot;homepageurl&quot;;

Could I capture the url of the previous page pass it in as a parameter? thoughts?


 
Sorry for the long delay in my reply!
Hopefully you have the e-mail notif. on ;)

Yep you could have a hidden field in the form, and when the page loads, set that field's value to

Request.ServerVariables(&quot;HTTP_REFERER&quot;)

Yes the spelling is correct heh.
Some firewalls will block this server variable, however, so don't rely on it too much.

After that just refer to the control in your javascript.

HTH
Elijah
 
Here's a slightly diff twist, same thing:

<script language=&quot;JavaScript&quot;>
function setFocus()
{
Form1.txtMyText.focus();
}
function PushIt(e){
if (e.keyCode == 13) {
Form1.buttMyButton.focus();
}
}
</script>

where the pushIt function allows the user to hit the Enter key to &quot;push&quot; the button...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top