I need to create a Javascript that does the following:
-
Detect when the first key is pressed in the RadEditor
-
Locate the cursor
-
Programatically insert a piece of text where the cursor is
-
Allow the normal process to continue, ie the key pressed by the user is to be added immediately after where I added the text.
I managed to do this for non-telerik normal text areas. But it is giving me big problems with telerik RadEditor.Net2.dll version 7.2.0.0
On clientload of rad editor I am adding a KeyPress event handler. However, in keypress method I am unable to get the cursor position. I am using this script:
function getCaret(editor) {
if (editor.selectionStart) {
return editor.selectionStart;
}
else if (!document.selection) {
return 0;
}
var c = "\001";
var sel = document.selection.createRange();
var dul = sel.duplicate();
var len = 0;
dul.moveToElementText(editor); <-- GIVES JAVASCRIPT EXCEPTION OVER HERE
sel.text = c;
len = dul.text.indexOf(c);
sel.moveStart('character', -1);
sel.text = "";
return len;
}
What do you recommend? Am I approaching this incorrectly for RadEditor? I really need to get this working!