Javascript - DOM - Text Selection

javascript-dom

http://stackoverflow.com/questions/22914075/javascript-error-800a025e-using-range-selector
https://msdn.microsoft.com/en-us/library/ms535872%28v=vs.85%29.aspx

Could not complete the operation due to error 800a025e
if (window.getSelection().getRangeAt(0).getClientRects.length > 0) {
    window.getSelection().removeAllRanges();
}

var range = document.body.createTextRange();
var retval = range.moveToElementText(element);
console.log (retval);
range.select();

jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    // console.log(this, element);
    if (typeof element == 'undefined') {
        return;
    }
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

function ExtendSelection() {
  var sel;
  if (document.body.createTextRange) { // Internet Explorer
      var range = document.body.createTextRange();
      range.moveStart('character', 0);
      // range.collapse(true); <-- IE does not understand this line
      range.moveStart('character', 29); //causes an exception 
      // range.moveStart('character', 30);//No exception 
      range.moveEnd('character', 177);
      range.select();
      alert(range.text);
  } else {
      alert("Your browser does not support this example!");
  }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License