How To Get Current Caret Position In A SCEditor Textarea?
I'm using SCEditor along jQuery in my website and I wonder how can I get the current caret position relative to the beginning of the textarea/div? I tried things like: $J('#textare
Solution 1:
You can use rangeHelper().saveRange() to insert markers at the start and end of the selection and work from them.
e.g.:
var sceditor = $("textarea").sceditor("instance");
// Inserts spans with the ID #sceditor-end-start and #sceditor-end-marker
// at the start and end of the current selection
sceditor.getRangeHelper().saveRange();
// Get the DOM node for #sceditor-end-marker and remove all
// nextSiblings and parent nextSiblings from the editor
// which will remove everything after the end of the selection
var node = sceditor.getBody().find('#sceditor-end-marker').get(0);
while (node) {
while (node.nextSibling)
node.parentNode.removeChild(node.nextSibling);
node = node.parentNode;
}
// Restores the selection back to the positions of the
// #sceditor-end-start and #sceditor-end-marker markers
sceditor.getRangeHelper().restoreRange();
sceditor.focus();
Post a Comment for "How To Get Current Caret Position In A SCEditor Textarea?"