tags:

views:

97

answers:

2

I have a website with 2 forms. One for search and another for the login. When I use the enter key to submit, the search is always called because it is the first form on the page.

What I want to do is program the enter key to click a certain button when a certain textbox has focus.

I'm using asp:textbox and asp:button for my login form.

A: 

Is the one form inside of the other, HTML-wise?

Joseph Mastey
There's actually 1 form tag around the whole site because you can only have 1 runat=server in a webpage.Basically it's a loginview and an asp:textbox and asp:button by themselves (which is for the search).
BioXhazard
+1  A: 

Create a global variable which tracks what form is in focus:

<script> var currentForm = document.forms[0];</script>
<form ...><input onfocus="currentForm = this.form;"/></form>
<form ...><input onfocus="currentForm = this.form;"/></form>

Attach a keyboard handler to the window, look for the Enter key, then when it's hit submit the form that's being focused:

 function globalKeyPressed(event) {
   if (event.keyCode == ENTER) { // This is pseudo-code, check how to really do it
     currentForm.submit();
   }
 }
levik
I don't understand the second part. Where is the globalKeyPressed declared?
BioXhazard
Somewhere in a script block in the page. You then have to make it the listener for global keypress events. Something like "window.onkeypress = globalKeyPressed;"
levik