Set text-cursor position in a textarea
<p>I'm working on a BBCode editor and here is the code:</p>
<pre><code>var txtarea = document.getElementById(&quot;editor_area&quot;);

function boldText() {
    var start = txtarea.selectionStart;
    var end = txtarea.selectionEnd;
    var sel = txtarea.value.substring(start, end);
    var finText = txtarea.value.substring(0, start) + '[b]' + sel + '[/b]' + txtarea.value.substring(end);
    txtarea.value = finText;
    txtarea.focus();
}
</code></pre>
<p>Everything is OK except one thing which is the position of the text-cursor. When I click on the boldText button, it sets the cursor position at the end of the Textarea!!</p>
<p>Actually, I want to be able to set the cursor position at a certain index. I want something like this:</p>
<pre><code>txtarea.setFocusAt(20);
</code></pre>