Mario Gobarola
Mario Gobarola

Reputation: 471

Submit form on Enter key with javascript

I'm not sure what I am doing wrong here. I want the enter key to work as well as clicking the button.

<form action="" method="get" class="priceOptionForm" name="priceOptionForm">
   <input name="paypal_email" type="text" value="whatever" id="email" />
   <a href="javascript:void(0);" class="bluebtn" id="profile_price" style="width:60px;margin-top:5px;">Save all</a>
</form>

Upvotes: 37

Views: 129250

Answers (11)

Tushar Saha
Tushar Saha

Reputation: 887

Since "keycode" is deprecated, use this:

document.getElementById('element').onkeydown = function (e) {
    if (e.key == 'Enter') {
        
    }
};

Upvotes: 1

Mam&#233;
Mam&#233;

Reputation: 69

The submit event fires when the user clicks a submit button (<button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form.submit() method directly. check documentation

Upvotes: 0

Alchemist
Alchemist

Reputation: 385

You can use onKeyDown in input tag.

Below is the details of it.

const handleKeyDown = (e) => {
   if(e.key === "Enter") {
        //form actions
   }
}

<form action="" method="get" class="priceOptionForm" name="priceOptionForm">
   <input name="paypal_email" type="text" value="whatever" id="email" onKeyDown={handleKeyDown} />
   <a href="javascript:void(0);" class="bluebtn" id="profile_price" style="width:60px;margin-top:5px;">Save all</a>
</form>

Upvotes: 1

Nandhu
Nandhu

Reputation: 9

You Can Put a Default Button ID in form tag is automatically trigger

<form id="form1" runat="server" defaultbutton="Button1">

<asp:Button ID="Button1" runat="server" Text="Button"

    OnClick = "Button1_Click" />

<asp:Button ID="Button2" runat="server" Text="Button"

    OnClick = "Button2_Click" />

<asp:Button ID="Button3" runat="server" Text="Button"

    OnClick = "Button3_Click" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</form>

Upvotes: 1

Waqas Tahir
Waqas Tahir

Reputation: 8262

simply make a hidden button like this
HTML

<input type="submit" id="submitbtn"  />

CSS

#submitbtn{display:none;}

when the user will hit the enter button the form will be submitted
Don't forget to put the type="submit"

Upvotes: 5

iGuest
iGuest

Reputation: 71

// Process form if use enter key. put script in head.    
document.onkeyup = enter;    
function enter(e) {if (e.which == 13) submitForm();}

// uses keyup not down as better practice imo    
// submitForm() is user function that posts the form

Upvotes: 1

Anooj VM
Anooj VM

Reputation: 2633

Please use below code snippet...It should be added into script block

<script>
    document.onkeydown=function(evt){
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if(keyCode == 13)
        {
            //your function call here
        }
    }
</script>

Upvotes: 19

夏至夕陽
夏至夕陽

Reputation: 362

All below codes should be added into script block or file. define submit function:

function submitForm(){
    document.priceOptionForm.submit();
    document.priceOptionForm.method='post';
}

For the enter key to submit form:

document.onkeydown=function(){
    if(window.event.keyCode=='13'){
        submitForm();
    }
}

For the link to work:

document.getElementById("profile_price").onclick=submitForm;

You can refer to http://jsfiddle.net/honglonglong/YMX2q/ for some trying.

Upvotes: 11

Viraj Shah
Viraj Shah

Reputation: 792

Oh that is because the HTML form element does not recognize the link as a button, to click.. you need to replace it with a button...

<input type="submit" value="this will display on your button" onClick="javascript:void(0)">

but if you want it to look like a link you should do this in the css

<style type="text/css">
input{background-color:white;border:0 none;}
</style>

Upvotes: 0

sbking
sbking

Reputation: 7680

Use an <input type="submit"> instead of a link. Then the enter key will work automatically.

Upvotes: 9

Ringo
Ringo

Reputation: 3967

Try this:

document.getElementById('email').onkeydown = function(e){
   if(e.keyCode == 13){
     // submit
   }
};

Upvotes: 41

Related Questions