title: Set cursor within text of input using jQuery
body: <p>Using this topic:
  <a href="https://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area">jQuery Set Cursor Position in Text Area</a></p>
  
  <p>I write this code but it does not works:</p>
  
  <pre><code>&lt;input id="myTextInput" type="text" value="some text2"&gt;
  &lt;input type="button" value="set mouse" id="btn" /&gt;
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>$(document).ready(function () {
      $('#btn').on('click', function () {
          var inp = $('#myTextInput');        
          var pos = 3;
          inp.focus();
          if (inp.setSelectionRange) {
              inp.setSelectionRange(pos, pos);
          } else if (inp.createTextRange) {
              var range = inp.createTextRange();
              range.collapse(true);
              if (pos &lt; 0) {
                  pos = $(this).val().length + pos;
              }
              range.moveEnd('character', pos);
              range.moveStart('character', pos);
              range.select();
          }
      });
  });
  </code></pre>
  
  <p><a href="http://jsfiddle.net/qeanb8gk/" rel="nofollow noreferrer">DEMO</a></p>
  
  <p>Where is my mistake?
  Thanks</p>