Placing cursor in textbox at specified length
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1865563/set-cursor-at-a-length-of-14-onfocus-of-a-textbox">Set cursor at a length of 14 onfocus of a textbox</a>  </p>
</blockquote>



<p>I have a code which lays the focus selecting certain length of substring in the text box. I want to place the cursor instead of selecting the entire string. the code is as below <br></p>

<pre><code>&lt;script type="text/javascript"&gt;
window.onload = function() {
    $("#message").keyup(function(e){
        var message = document.getElementById('message');
        // Select a portion of text
        createSelection(message, 0, 5);

        // get the selected portion of text
        var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
        alert(selectedText);
    });
    function createSelection(field, start, end) {
    if( field.createTextRange ) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end-start);
        selRange.select();
    } else if( field.setSelectionRange ) {
        field.setSelectionRange(start, end);
    } else if( field.selectionStart ) {
        field.selectionStart = start;
        field.selectionEnd = end;
    }
    field.focus();
    }}

&lt;/script&gt;
</code></pre>

<p>HTML part of the code<br></p>

<pre><code>&lt;input type="text" name="message" id="message" value="" /&gt;
</code></pre>
