Question

I'm executing these simple rows of javascript in latest ie 11, just to select all content of a div

Here a screenshot from ie11 dev tool

Code and error - screenshot

Like you can see, IE alter me saying: "unable to complete the operation due to error 800a025e".

I'm not able to understand the nature, the source, of the problem, and no others stack overflow questions I read give me a clear answer.

This is full code of my selectText jQuery "personal" extension

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);
    }
};

Element in this case is a

[Object HTMLTableElement]

Edit 1: with a little mod:

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

I'm able to say you retval is undefined. This code work on ff without problems, so element selector is fine. jQuery version is 1.11

Was it helpful?

Solution

I resolved using this trick:

I wrapped the html table with a DIV

I use the same code, as is, with no changes, against the DIV, instead of using for selecting only the table.

It's working.

Probably html table is not a selectable text for IE

OTHER TIPS

None of the above suggestions or hints properly resolves this particular problem.

The issue itself is caused by non-text (for example table) elements getting collected together with other elements within a TextRange collection. Attempting to clear the collection while these table elements remain within the collection will unconditionally cause the reported error above.

The issue affects IE 9/10/11, and there are specific workarounds for each of those browsers. Unfortunately those workarounds are not compatible with other browsers (firefox, safari, chrome, edge), so we need to special case the clear handling for IE 9/10/11 while providing alternate handling for more standards compliant browsers. The following appears to work everywhere, at least as of this writing:

      if (doc.body.createTextRange) { // All IE but Edge
        var range = doc.body.createTextRange();
        range.collapse();
        range.select();
      }
      else {
        doc.getSelection().removeAllRanges();
      }

The IE specific branch is a roundabout way of clearing a range containing table elements without actually invoking the empty() method which otherwise results in the troublesome exception.

I got this error when trying to window.getSelection().removeAllRanges(); and there was no selection. One workaround is to check if there is a selection first:

if (window.getSelection().getRangeAt(0).getClientRects.length > 0) {
    window.getSelection().removeAllRanges();
}

Also, this question is a duplicate of Could not complete the operation due to error 800a025e, but I couldn't mark it as such because the other question doesn't have an accepted answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top