How to trigger back space with jQuery?

I have the below code which triggers various events on keydown

    $('#input-div').keydown(function(e) {
if(e.which == 13) {
    userInput = $(this).val();

this clears up input box. But since the enter key is pressed, the cursor goes to the second line. I want the cursor to go back to the initial position after the enter key is released; on key up.

any help?

Answers 1

  •     <!DOCTYPE html>
      <form method="POST" id="userform">
          <input type="text" name="firstname" id="firstname">
          <input type="text" name="lastname" id="lastname">
      <script type="text/javascript">
           return false;    
        $("#userform input").keydown(function(e) {
            if(e.which == 13) {
                userInput = $(this).val();
                $("#userform input#firstname").focus();

    you may use .focus() to focus on specific input box

Related Articles