0

We are migrating from javascript to typescript and some of the functions from document class doesn't seem to exists any more. I can't find anything equivalent.

 1) createRange()
          var textRange = document.getSelection().createRange();
 2)createTextRange
          var preCaretTextRange = document.body.createTextRange();
 3)focus()
        var el : Node= document.getElementById(divId).childNodes[0];
           el.focus();

Picture is worth a thousand words

user911
  • 1,509
  • 6
  • 26
  • 52

1 Answers1

2

(1) The Selection object doesn't have the createRange method (can also be seen in the lib.d.ts definition).
Instead you should use document.createRange:

var textRange = document.createRange();

(2) The createTextRange is an IE only thing (CreateTextRange is not working in Chrome), it's not a standard.
There's an issue for that: 'createTextRange' seems to be missing from 'lib.dom.d.ts' and other including libs which says:

this is a break because this is an IE-only API. Users can augment the interfaces if necessary

So you can do this:

interface TextRange {
    ...
}

interface HtmlElement {
    createTextRange(): TextRange;
}

(3) The focus() part works well, the compiler has no issues with it.


Edit

The Node object doesn't have the focus() method, so you need to cast it:

var el = document.getElementById(divId).childNodes[0] as HTMLElement;
el.focus(); // works now
Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thank you! focus() issue is still there , I have updated the question. How do I resolve issue #2? – user911 May 18 '17 at 16:05
  • Check my revised answer concerning `focus`, as for the 2nd issue, as the quote says you need to "augment the interfaces", and I gave you a short snippet as an example of how to do that – Nitzan Tomer May 18 '17 at 16:20
  • I was directed to this answer, but I still get compile errors, such as `Argument of type 'Range' is not assignable to parameter of type 'TextRange'.` – yondaimehokage May 24 '17 at 18:17
  • @yondaimehokage There's no such type as `TextRange`, are you adding it yourself as shown in the answer? – Nitzan Tomer May 24 '17 at 18:25