Friday, May 2, 2014

How to Submit a form when enter key is pressed

Sometimes in a form we want to submit a form when we press enter key(we do not want to click the button).

For this purpose here is a simple code

Suppose we have a page as below

<div id="loginDiv" title="Registered User Sign-In">
    <h1 class="upp">Sign In</h1>
    <form action="Login.do" name="formLogin" method="post" id="formLogin">
   
            <p>Please complete these fields.</p>
            <p>
            <input name="email" type="text" placeholder="Email address" size="30"  />
            </p>
            <p>
<input name="password" type="password" placeholder="Password" size="30" />
            </p>
           
<p class="clear"></p>
<p>
<input type="button" name="login" id="login" onclick="$('#formLogin').submit()" value="Sign In" >
</p><!-- bigbut -->
</form>

</div>

The id of div is  loginDiv . So we can write the following code for capturing key press event on div.

$("#loginDiv").keypress(function(e){
 if (e.keyCode == 13){
 $('#searchForm').submit();
 }
});


above code solves this problem.

No comments:

Post a Comment