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

Posted on 2007-09-28
Last Modified: 2012-06-21
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;

Question by:dhite99
    LVL 14

    Accepted Solution

    Do this...

    <textarea onkeypress="if (event.keyCode == 13) {this.form.submit();}">
    LVL 13

    Assisted Solution

    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.
    LVL 14

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A common challenge, or question, when working with two calendar dates is: What is the difference between the specified days? As is frequently the case, the answer is: It depends... What kind of difference do you want? Given two date obje…
    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now