Question

i know how to execute execCommand on some selected text, but is there a way to execute on some other text in my contenteditable div ?

Was it helpful?

Solution

In IE, yes. In other browsers, no.

All major desktop browsers implement some form of document.execCommand(), which acts only on the user selection. In IE, the TextRange object also has an execCommand method. For example, the following will turn the text contents of an element green when the user hovers the cursor over it:

<div id="test">Here is a test div</div>

<script type="text/javascript">
    var div = document.getElementById("test");
    div.onmouseover = function() {
        if (document.body.createTextRange) {
            var textRange = document.body.createTextRange();
            textRange.moveToElementText(div);
            textRange.execCommand("foreColor", false, "green");
        }
    };
</script>

OTHER TIPS

In case you are not aware, the .execCommand is an IE only non-standard javascript function that should be avoided.

If you tell us what you want to use it for, then we could suggest a standards-compliant alternative.

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