title: jQuery Set Cursor to Beginning of Input Field on Focus
body: <p>I have an input field <code>&lt;input value="@website.com" type="text" name="email" id="email"&gt;</code> and I want to make it so when a user focuses on this field, it will move the cursor to be before the <code>@website.com</code>.</p>
  
  <p>I have the following Javascript/jQuery code but it does not seem to work:</p>
  
  <pre><code>$(document).ready(function() {
      $("#email").focusin(function(){
          $("#email").focus();
          $("#email")[0].setSelectionRange(0,0);
      });
  });
  </code></pre>
  
  <p>It seems like the <code>.focus()</code> keeps calling the <code>focusin()</code> function.</p>
  
  <p>I know <code>$("#email")[0].setSelectionRange(0,0);</code> is the proper way to move the cursor to the front but how do I bind it to when the field has focus?</p>
  
  <p>Edit: So when I use:</p>
  
  <pre><code>$("#email").focus(function(){
      $("#email")[0].setSelectionRange(0,0);
  });
  </code></pre>
  
  <p>It sets the cursor to the beginning when I tab into the field but not when I click in.</p>
  
  <p>Edit2: This is different than <a href="https://stackoverflow.com/questions/2897155">enter link description here</a> because that just gets the caret position whereas I am looking to set the caret position.</p>