You are here: Reference > JavaScript > client-side > selection and ranges > methods > pasteHTML (TextRange)

pasteHTML method (TextRange)

Browser support:
Replaces the contents of the current TextRange object with the specified HTML formatted text.
The text property sets or retrieves the text content of a TextRange object as a string. If you need the contents in HTML format, use the htmlText property. To replace the contents of a TextRange object with a HTML formatted string, use the pasteHTML method.


object.pasteHTML (HTMLText);
You can find the related objects in the Supported by objects section below.


Required. String that specifies the HTML formatted text to paste.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the pasteHTML method:
    <script type="text/javascript">
        function MoveButton () {
            var wanderer = document.getElementById ("wanderer");
            if (window.getSelection) {  // all browsers, except IE before version 9
                var selection = window.getSelection ();
                if (selection.rangeCount > 0) {
                    var range = selection.getRangeAt (0);
                    range.collapse (false);
                    range.insertNode (wanderer);
            else {  // Internet Explorer before version 9
                var textRange = document.selection.createRange ();
                textRange.collapse (false);
                textRange.pasteHTML (wanderer.outerHTML);
                wanderer.parentNode.removeChild (wanderer);
    <div onmouseup="MoveButton ()" style="width:400px; background-color:#e0f0d0;">
        Select some text with your mouse within this field.
        When the left button is released the wanderer button is placed 
        at the ending of the selection.
        Left mouse clicks also move the wanderer button in Internet Explorer, Firefox, Google Chrome and Safari.
    <button id="wanderer">wanderer</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content