3

I am using angular 5 testing in IE 11. I need to use document.selection in my typescript file but it is giving it as undefined. I have also declared the selection globally as shown below but even then there is no luck. Can anyone please help me to fix it ?

declare global{
    interface Document {
        selection?: any;
    }
}





var key = event.which || event.keyCode;
        if(key === 37 || key === 39){        
            event.stopPropagation();     
            let inputDocument = document.getElementById('numericinput');   
            let range = document.createRange();
            range.collapse(true);                       
            let iCaretPos = 0;
            if(key === 39 && this.iCaretPos < inputDocument.innerHTML.length){
                 console.log("inside if");
                 this.iCaretPos = this.iCaretPos +1;
                 range.setEnd(input.firstChild, 0);      
                 range.setStart(input.firstChild, this.iCaretPos);   
            }else if(key === 37 && iCaretPos > 0){
                 console.log("inside else");
                 this.iCaretPos = this.iCaretPos -1;
                 range.setEnd(input.firstChild, 0);      
                 range.setStart(input.firstChild, this.iCaretPos);  
            }  
            window.getSelection().removeAllRanges;      
            window.getSelection().addRange(range);  
    }
}
Nithin
  • 371
  • 7
  • 29
  • please provide a [mcve] of your issue. –  May 09 '19 at 08:39
  • 1
    Interface declares just type not a property. So if it's absent in IE it either should be created via polyfills or chosen another property analogue in that specific browser. – Sergey May 09 '19 at 08:40
  • did you try searching stack overflow ... https://stackoverflow.com/questions/21777728/ie-11-document-selection-does-not-work - the change fully documented my MicroShaft - https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dev-guides/bg182625(v=vs.85)#legacyAPIs – Jaromanda X May 09 '19 at 08:41
  • @Sergey - document.selection is MicroShaft's proprietary rubbish, they removed it, starting with IE11 and went to established standards that work in every browser – Jaromanda X May 09 '19 at 08:41
  • @JaromandaX Thanks for the update. Yes, document.getSelection() is working. It allows to create range but it does not have methods like moveStart or moveEnd. I have a requirement to move the cursor of the input text character by character based on keyboard left/right options using Range. I have updated the code above. Can you please let me know if it is possible to achieve it ? – Nithin May 09 '19 at 08:56
  • Can you post your whole sample. So we can see it and try to test it on our side to check the result. The code I suggest you in this thread is working fine for me in IE 11. https://stackoverflow.com/questions/56041720/angular-5-how-to-move-the-cursor-character-by-character-using-keyboard-left-ri – Deepak-MSFT May 09 '19 at 13:48
  • Hi, Thanks for the support. I resolve it using input.setSelectionRange – Nithin May 12 '19 at 08:48
  • From your last comment, It looks like you had found the solution for your issue. I suggest you to post your solution as an answer and try to mark your own answer as an accepted answer for this question after 48 hrs, when it is available to mark. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Deepak-MSFT May 13 '19 at 07:30
  • I will do it. Thanks ! – Nithin May 13 '19 at 08:08

0 Answers0