1

How do I make my button work by pressing enter?

I created a form button and instead of clicking the "log in" button

Is their a way to make the "enter" button on my computer also click to the "log in" button.


Code from the comments

<form name = "myform"> Password: <input type="password" name="pword"> <input type="button" value="Log In" name="Submit" onclick= "validate()"> </p> </form> 
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
user2773016
  • 31
  • 1
  • 1
  • 4

2 Answers2

3

You tag mentions ASP.NET, if you do use ASP.NET the easiest way is to specify Default Button for the form:

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

    <asp:button ID="Button1" runat="server" text="Button"   />            

</form>

The example above will specify Button1 as a button that will submit the form when Enter is pressed.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • 1
    Thank you, from the code I posted where would I put this code. I tried a few different variations and it dosent seem to work – user2773016 Nov 07 '13 at 14:36
  • 1
    @user2773016 so you do not use ASP.NET forms and controls (with attribute `runat="server"`? Just to confirm - you use plain HTML Form/Input elements, *not* ASP.NET ? – Yuriy Galanter Nov 07 '13 at 14:58
  • 1
    Yes just plain html elements. The button works fine when I click it, but when I press enter it does nothing – user2773016 Nov 07 '13 at 21:28
  • 1
    Then you have to use JavaScript solution provided in the link by @EmmadKareem (Here's direct link to answer http://stackoverflow.com/a/12263092/961695 use second approach) – Yuriy Galanter Nov 07 '13 at 21:31
0

Instead of making your Login Button type "button". I would think just by putting a simple input with the type submit would be the best way. Then anytime you push enter within the text inputs it will automatically submit the data.

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

   <input type="submit"  value="Log In!" />

</form>