|
-
Sep 7th, 2011, 06:01 PM
#1
Thread Starter
Member
[RESOLVED] Get the position of the cursor in a textarea
Hi,
I've done the following code to place formatting tags around code in a textarea. It works perfect in Firefox but in IE it only works if some text is selected.
How do I get the positon of the cursor if no text is selected?
HTML Code:
<script type="text/javascript">
//<![CDATA[
function ApplySimpleTag(tagCode) {
var selText = "";
var beforeText = "";
var afterText = "";
var newSelStart = 0;
var commentBox = objById('pbertie_CommentBox');
var StartTag = '[' + tagCode + ']';
var EndTag = '[/' + tagCode + ']';
if (commentBox.selectionStart) {
// check whether some text is selected in the textarea
beforeText = commentBox.value.substring(0, commentBox.selectionStart);
afterText = commentBox.value.substring(commentBox.selectionEnd);
newSelStart = commentBox.selectionStart + StartTag.length;
if (commentBox.selectionStart != commentBox.selectionEnd) {
selText = commentBox.value.substring(commentBox.selectionStart, commentBox.selectionEnd);
}
commentBox.value = beforeText + StartTag + selText + EndTag + afterText;
if(commentBox.setSelectionRange) {
commentBox.setSelectionRange(newSelStart, newSelStart + selText.length);
}
else if (commentBox.createTextRange) {
var selRange = commentBox.createTextRange();
selRange.collapse(true);
selRange.moveStart("character", newSelStart);
selRange.moveEnd("character", selText.length);
selRange.select();
}
}
else {
var textRange = document.selection.createRange();
if (textRange.parentElement() === commentBox) { // Fails this check is the selected length is zero.
textRange.text = StartTag + textRange.text + EndTag;
}
}
commentBox.focus();
}
// ]]>
</script>
<textarea class="pbertie_EditorTextarea" id="pbertie_CommentBox"></textarea>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|