0

I want to select a text in GWT:

I have a method which select the text in Firefox but bug in Chrome:

public native void markThisText(Element elem) /*-{
        if ($doc.selection && $doc.selection.createRange) {
            var range = $doc.selection.createRange();
            range.moveToElementText(elem);
            range.select();
        } else if ($doc.createRange && $wnd.getSelection) {
            var range = $doc.createRange();
            range.selectNode(elem);
            var selection = $wnd.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
        }
}-*/;

Error log :

Caused by: com.google.gwt.core.client.JavaScriptException: (INVALID_NODE_TYPE_ERR):  
INVALID_NODE_TYPE_ERR: DOM Range Exception 2    at    com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248
)   at   
com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)   at  
com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)     at 
com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289)     at 
com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)

Do you know why? Thanks

RAS
  • 8,100
  • 16
  • 64
  • 86
superscral
  • 480
  • 1
  • 11
  • 33

1 Answers1

0

You may find this helpful:

Highlight text inside of a textarea

Community
  • 1
  • 1
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Your example get a taxt selected and not select a text, isn't it? And i've got a "Cannot read property 'getSelection" of undefined" – superscral Oct 30 '12 at 15:13
  • Sorry, I misunderstood the question. Take a look at the similar question - it has a good answer. – Andrei Volgin Oct 30 '12 at 16:57
  • It's not hep me very much... As i read on the web, there is an issue for webkit browsers (safari and chrome) with range.setNode... – superscral Oct 31 '12 at 07:39