Blainer
Blainer

Reputation: 2712

Submit form on enter

How do I make this submit on pressing the enter key? I want to completely remove the submit button.

HTML

<form class="iform">
<input id='input1' name='input1'/>
<input type='button' value='GO!' id='submit' onclick='onSubmit()' />
</form>

JS

$('#submit').click(function() { 
$('#slider').anythingSlider($("#input1").val()); 
});

Upvotes: 0

Views: 16154

Answers (6)

damoiser
damoiser

Reputation: 6238

A simpler solution without the submit button:

HTML

<form action="path_to_the_action">
  <input class="submit_on_enter" type="text" name="q" placeholder="Search...">
</form>

jQuery

<script type="text/javascript">
  $(document).ready(function() {
    $('.submit_on_enter').keydown(function(event) {
        if (event.keyCode == 13) {
            this.form.submit();
            return false;
         }
    });
  });
</script>

Upvotes: 0

mplungjan
mplungjan

Reputation: 178384

Very simple since you have only one field

EITHER

<form class="iform" id="iform">
<input id='input1' name='input1'/>
<input type='submit' value='GO!'  />
</form>

OR even

<form class="iform" id="iform">
<input id='input1' name='input1'/>
</form>

JS in either case

$('#iform').submit(function(e) {   
  $('#slider').anythingSlider($("#input1").val());
  e.preventDefault(); 
});

Upvotes: 3

pensan
pensan

Reputation: 420

a normal form is automatically submitted when pressing enter.

<form id="form1" action="test.php">
    <input type="text" name="input1" />
</form>

this should work fine, but it may not validate.

If you want to submit your form in javascript, you can use the .submit() function.

document.forms["form1"].submit();

or jQuery

$("#form1").submit();

Upvotes: 1

Dau
Dau

Reputation: 8858

use this

<input type="submit" style="display:none;">

Upvotes: 0

Jorge Zapata
Jorge Zapata

Reputation: 2336

Try this:

$('#formId').keypress(function(e){
      if(e.which == 13)
      $('#slider').anythingSlider($("#input1").val());            }
     return false;
      });

Upvotes: 1

No Results Found
No Results Found

Reputation: 102854

Hitting "enter" while focused in the text field will already submit the form, you don't actually need the submit button (look at Stack Overflow's "search" form at the top right of this page for example). You just might need to change your javascript to listen to the submit event instead:

$('form').submit(function() { 
   $('#slider').anythingSlider($("#input1").val()); 
});

If you want the form submitted when someone presses enter regardless of where the focus is, I would suggest against it. It's extremely confusing behavior and can easily be triggered by accident.

Upvotes: 4

Related Questions