0

This is the suggested answer from the Javascript SO question I have modified it to make it a JSNI:

Element content = DOM.createElement("code");
select(content);

public static native void select(Element el)/*-{
    var doc =  $wnd.document
        , text = $wnd.$(el)
        , range, selection
    ;
    if (doc.body.createTextRange) {
        range = $wnd.document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if ($wnd.window.getSelection) {
        selection = $wnd.window.getSelection();
        range = $wnd.document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
}-*/;

However when used, it throws:

caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) : Argument 1 of Range.selectNodeContents does not implement interface Node.

What could be wrong with my code?

Community
  • 1
  • 1
quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

0

Pass the id of your element which you can use in the Javascript code to acquire the respective HTML element, and pass this element to your selectNodeContents() javascript function:

// Call it like this:
select(el.getId());

// And the modified select:
public static native void select(String elId)/*-{
    // ...OTHER CODE OMITTED
    range.selectNodeContents($wnd.document.getElementById(elId));
    // ...OTHER CODE OMITTED
}-*/;
icza
  • 389,944
  • 63
  • 907
  • 827