• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

How to submit form using <enter> key & without using a "type=submit" button

I have a form that has a text entry field. I am able to trap the onkeydown & enter key, but instead of triggering a click on a submit button, I'd like to submit the form wihtout having a "type=submit" button. Is there some way to, once the enter button is trapped, submit the form without the use of a submit button? Alternatively, is there some way to have a "type=submit" that is hiddent?
Here is the enter key trapping code and the form definition - note that I have it doing a click on the submit button, but I need to get rid of the button from the form and just take "enter" as submitting the form.

      $html = '<html>';
      $html .='<head>';
      $html .='<script type="text/javascript" language=javascript>';
      $html .= '<!--';
      $html .='document.onkeydown = fnTrapKeyDown;';
      $html .='function fnTrapKeyDown(e)';
      $html .='{ var intKeyPressed = document.all? event.keyCode: e.which;';
      $html .=' if(intKeyPressed == 13)  { document.getElementById(\'submit\').click();';
      $html .='return false;}';
      $html .='}';
      $html .='//-->';
      $html .='</script>';
      $html .='</head>';
      $html .='<body bgcolor=#d0d0d0><img border="0" src="inc/pnlogo.jpg" name="pnlogo" /><br />';
      $html .='<form action="maleftp.php" method="post">';
      $html .='<input type="text" name="smartsearch" size="10" maxlength="20" /><br />';
// want to get rid of the following submit button, need some way to submit form without having a submit button
      $html .='<input name="submit" type="submit" value="submit"/>';
      $html .='</form>';
                      echo $html;

  • 2
2 Solutions
Do this...

<textarea onkeypress="if (event.keyCode == 13) {this.form.submit();}">
The following style hides this button.
<input type="submit" style="display: none; " />

Alternatively you could use an image as submit button, a transparent 1 pixel image.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now