I am currently working on a small script that allows people to insert BB-codes to edit their text. They are able to click a button (for example underline) and then the bb-code will appear at the end of the textfield-value.

How can i retrieve the current position of the cursor within the textarea and then parse the bb-code within? This is a small part of the code:

<script type="text/javascript">
function addu() {
var message = window.prompt("Enter the underlined text below:");
if (message != "" && message != "null" && message != "undefined") {
var newtext = document.editform.text.value;
newtext += "\[u\]" + message + "\[/u\]";
document.editform.text.value = newtext;
<img src="ubutton.jpg" onClick="addu()" />
<form action="edit.php">
<textarea class="editarea" name="text" id="text" cols="100" rows="33"></textarea>
<input type="submit" name="savebutton" value="save" />

Does anyone have a suggestion?


Look into this link.


I have looked into the link but the code doesnt work in FF (first time ever that happend!) and works in IE. Although it does figure out which line and which column, it can't figure out what the current char is (and this is required if i want to substr the first part of the text, place a bb-code between, and then place the second part of the text next to it.)