focus at the end of the text in a textbox
<p>i have a requirement: a text box can have a max of 50 characters, to achieve this I gave preventDefault.   </p>

<pre><code>if (!(event.ctrlKey &amp;&amp; (event.which == 65 || event.which == 86 || event.which == 67 || event.which == 88))
                &amp;&amp; (!(event.shiftKey &amp;&amp; (event.which == 35 || event.which == 36)))
                &amp;&amp; (key != 8 &amp;&amp; key != 37 &amp;&amp; key != 39 &amp;&amp; key != 46 &amp;&amp; key != 36 &amp;&amp; key != 35 &amp;&amp; key != 33 &amp;&amp; key != 34)) {
                    event.preventDefault();
                }
</code></pre>

<p>But now the requirement is : when we press ctrl+a and then abc it should erace everything from the text and print abc.<br>
to achieve this i wrote this code ->  </p>

<pre><code>var focusItem = $('&lt;input type="text"&gt;');
     function getSelText() {
              var txt = '';
              //IE
        var focusItem = $('&lt;input type="text"&gt;');
         if (document.selection != undefined) {
             focusItem.focus();
             var sel = document.selection.createRange();
             var rangeParent = sel.parentElement();
             txt = sel.text;
             if (!(event.ctrlKey &amp;&amp; (event.which == 65 || event.which == 86 || event.which == 67 || event.which == 88))
             &amp;&amp; (!(event.shiftKey &amp;&amp; (event.which == 35 || event.which == 36)))
             &amp;&amp; (key != 8 &amp;&amp; key != 37 &amp;&amp; key != 39 &amp;&amp; key != 46 &amp;&amp; key != 36 &amp;&amp; key != 35 &amp;&amp; key != 33 &amp;&amp; key != 34)) {
             //event.preventDefault();
            if (txt.length &gt; 0 &amp;&amp; ((key &gt;= 65 &amp;&amp; key &lt;= 90) || (key &gt;= 48 &amp;&amp; key &lt;= 57) || (key &gt;= 97 &amp;&amp; key &lt;= 122))) {
                sel.text = String.fromCharCode(key).toLowerCase();
                }
                txt = sel.text;
             }
          }                        
          return txt;
       }
</code></pre>

<p>now things are working but.. when I press ctrl+a abc then its getting printed as bca. Whats happening is : When i press a after ctrl+a then a is getting printed on the textbox but the focus is getting set before the text, that is a.<br>
How to deal with this.<br>
I am using windows 7, IE9, visual studio 2010.<br>
Thanks.</p>
