I am using JavaScript to create a very simple code that inserts bulletin board codes into a textarea when you click on a button.
The code I wrote works ok, however, I would like to be able to place the new tags at the cursor instead of at the end of the text, and then keep the cursor in the middle of the new tags.
For example: currently, when a user clicks b, then u, then s, it displays as [b][/b][u][/u][s][/s]
. I would like to be able to do something like [b][u][s]^[/s][/u][/b]
where ^
is the cursor. Is there any easy way to do this?
<script type="text/javascript">
function addTag(prefix, suffix){
texteditor = document.getElementById("texteditor");
texteditor.innerHTML += '[' + prefix + ']' + '[' + suffix + ']';
}
</script>
<ul class="wysiwyg">
<li><a href"#" title="Bold" class="bold" onclick="addTag('b', '/b'); return false;"></a></li>
<li><a href"#" title="Underline" class="underline" onclick="addTag('u', '/u'); return false;"></a></li>
<li><a href"#" title="Strike Through" class="strikethrough" onclick="addTag('s', '/s'); return false;"></a></li>
<li><a href"#" title="Italicize" class="italics" onclick="addTag('i', '/i'); return false;"></a></li>
</ul>