press enter to submit a form or equal to a click on a button in a form

i have a search box and 2 buttons on a form. what i want to do is after i type in the search box, i want to be able to hit enter and it will automatically knows button 2 is the search button because button 1 is a clear button. how do i do that. my page is in html and vbscript? thanks.
StewSupportAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kalpana_NatarajanConnect With a Mentor Commented:
If you want  to handle a submit form action, when the user presses enter on any of your textfields,

1) Enable the keydownevent for the document:

document.onkeydown = processKeyDownEvent()

2) define the function processKeyDownEvent

function processKeyDownEvent()
{
  var eventObject = window.event;
  switch(eventObject.keyCode)
  {
    case 13:
         //submit form code
         break;
   }
}


To get the source element on which the event occured, eventObject.target or eventObject.sourceElement will do the needful.

Please let me know if anything is unclear.

Regards,
Kalpana
0
 
sam2912Connect With a Mentor Commented:
Did you mean this:
<form action="" method="get">
<input name="" type="text" value="Try Me" /> <br />
<input name="" type="reset" /> <input name="" type="submit" />
</form>

Open in new window

0
 
StewSupportAuthor Commented:
yes
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
StewSupportAuthor Commented:
sorry i meant when i hit enters it will know that i want to submit the search not to reset.
0
 
sam2912Commented:
The code provided above will submit the form when you press enter after filling in the textbox.
0
 
StewSupportAuthor Commented:
i see but if i just have submit buttons and only want one.
0
 
sam2912Commented:
Did you mean:
I have more than one submit button and I want only one of them to submit the form?
0
 
Kalpana_NatarajanCommented:
No. What I replied was nothing to do with the submit button. I explained how to make hitting enter on any of the text fields or controls of the form to invoke submit form action, which is equivalent to hitting the subtmit button of the fomr.

Take for isntance a email accuont login screen. Type the username and perss enter, the page submits the form data. The user need not have to navigate to the submit button and press it, or use the mouse to navigate to the button and clickit.

hope this is clear
0
 
sam2912Commented:
Then the code I posted in the first post will do it. Just make sure you got a form field and a submit button inside. When you hit enter anywhere in the form it will submit it.
0
All Courses

From novice to tech pro — start learning today.