I need to select some characters from current cursor position. Say the user has "cat" in the editor. Now I would like to call a function at this point like "Select(-1)" and 't' should get selected.
BACKGROUND
----------
I am trying to create a keyboard layout plugin for Bengali language. So I am capturing the keypress and keydown events and replacing the keys with Unicode characters of that language. In some cases I need to replace already inserted character with a new one.
For example the user typed: "cat", then he types "d". This will replace the "t" in "cat" with "cafd", instead of only inserting "d". This example is only an illustration, Unicode character and a set of rules are used for the main purpose.
I was thinking to select 't' and replace with "fd".
Exiting Implimentation
----------------------
I do the same thing for a plain text box with the help of following function
function RemoveNInsert(field, value, len)
{
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
if (field.value.length >= len)
{
sel.moveStart('character', -1*(len));
}
sel.text = value;
sel.collapse(true);
sel.select();
}
else if (field.selectionStart || field.selectionStart == 0) {
field.focus();
var startPos = field.selectionStart-len;
var endPos = field.selectionEnd;
var scrollTop = field.scrollTop;
startPos = (startPos == -1 ? field.value.length : startPos );
field.value = field.value.substring(0, startPos)
+ value
+ field.value.substring(endPos, field.value.length);
field.focus();
field.selectionStart = startPos + value.length;
field.selectionEnd = startPos + value.length;
field.scrollTop = scrollTop;
} else {
var scrollTop = field.scrollTop;
field.value += value;
field.focus();
field.scrollTop = scrollTop;
}
} // end function RemoveNInsert
BACKGROUND
----------
I am trying to create a keyboard layout plugin for Bengali language. So I am capturing the keypress and keydown events and replacing the keys with Unicode characters of that language. In some cases I need to replace already inserted character with a new one.
For example the user typed: "cat", then he types "d". This will replace the "t" in "cat" with "cafd", instead of only inserting "d". This example is only an illustration, Unicode character and a set of rules are used for the main purpose.
I was thinking to select 't' and replace with "fd".
Exiting Implimentation
----------------------
I do the same thing for a plain text box with the help of following function
function RemoveNInsert(field, value, len)
{
if (document.selection)
{
field.focus();
sel = document.selection.createRange();
if (field.value.length >= len)
{
sel.moveStart('character', -1*(len));
}
sel.text = value;
sel.collapse(true);
sel.select();
}
else if (field.selectionStart || field.selectionStart == 0) {
field.focus();
var startPos = field.selectionStart-len;
var endPos = field.selectionEnd;
var scrollTop = field.scrollTop;
startPos = (startPos == -1 ? field.value.length : startPos );
field.value = field.value.substring(0, startPos)
+ value
+ field.value.substring(endPos, field.value.length);
field.focus();
field.selectionStart = startPos + value.length;
field.selectionEnd = startPos + value.length;
field.scrollTop = scrollTop;
} else {
var scrollTop = field.scrollTop;
field.value += value;
field.focus();
field.scrollTop = scrollTop;
}
} // end function RemoveNInsert
RE: How to select some characters
In this regard, I guess my FCK object is not correct. It has only one child node. Am I doing anything wrong?