Sunday, June 22, 2014

trying to find out the cursor index in GWT RichTextArea

getCursor  is available in GWT textarea and workd perfectly but I need to make the selected text Colored , Thats Why I am switching to RichTextArea .

If there is anyway i can give color to the text of TextArea please suggest 

if not Please guide me how to get cursor position in GWt RichtextArea.

I found One Solution using Javascript .(JSNI) with below code.

But its giving me the cursor position as per each line , for example If I am on 1st line of my RichtextArea on 6th word it will say "6"(which is correct), but if I am on my 6th word on 3rd line it will again say "6" which is Wrong , That index should be the total of all three rows text.
Can you please modify this Javascript or give me any other solution 

I have not much experience with Javascript

public static native int getSelection(Element elem) /*-{
                                                   var txt = "";
                                                   var pos = 0;
                                                   var range;
                                                   var parentElement;
                                                   var container;
                                                   if (elem.contentWindow.getSelection) {
                                                   txt = elem.contentWindow.getSelection();
                                                   pos = elem.contentWindow.getSelection().getRangeAt(0).startOffset;
                                                   } else if (elem.contentWindow.document.getSelection) {
                                                   txt = elem.contentWindow.document.getSelection();
                                                   pos = elem.contentWindow.document.getSelection().getRangeAt(0).startOffset;
                                                   } else if (elem.contentWindow.document.selection) {
                                                   range = elem.contentWindow.document.selection.createRange();
                                                   txt = range.text;
                                                   parentElement = range.parentElement();
                                                   container = range.duplicate();
                                                   container.setEndPoint('EndToEnd', range);
                                                   pos = container.text.length - range.text.length;
                                                   return pos;

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment