2

Can we change the HTML or its attributes of the selected part of the web page using javascript?

For example, There is a random web page:(A part of it is shown)

enter image description here

with HTML as

...<p>Sample paragraph</p>..

It is possible to get the HTML of the selected text which has already been answered here.

But, is it possible for me change the html of the selected text? Like, add a class or id attribute to the paragraph tag.

Community
  • 1
  • 1
Phani Rahul
  • 840
  • 7
  • 22

2 Answers2

3

The jQuery wrapselection plugin sounds like what you're looking for http://archive.plugins.jquery.com/project/wrapSelection . What you're asking for is not directly possible though, the plugin works by adding in extra nodes around the surrounding text which may not be 100% reliable (particularly in IE6-8)

Michael Low
  • 24,276
  • 16
  • 82
  • 119
2

Here is a working example without a plugin.

http://jsfiddle.net/9HTPw/2/

function getSel() {
    var sel = null;
    if (
    typeof document.selection != "undefined" && document.selection 
                                   && document.selection.type == "Text") {
        sel = document.selection;
    } else if (
    window.getSelection && window.getSelection().rangeCount > 0) {
        sel = window.getSelection();
    }
    return sel;
}

var getSelectionStart = (function () {

    function createRangeFromSel(sel) {
        var rng = null;
        if (sel.createRange) {
            rng = sel.createRange();
        } else if (sel.getRangeAt) {
            rng = sel.getRangeAt(0);
            if (rng.toString() == "") rng = null;
        }
        return rng;
    }

    return function (el) {
        var sel = getSel(),
            rng, r2, i = -1;
        if (sel) {
            rng = createRangeFromSel(sel);
            if (rng) {

                if (rng.parentElement) {
                    if (rng.parentElement() == el) {
                        r2 = document.body.createTextRange();
                        r2.moveToElementText(el);
                        r2.collapse(true);
                        r2.setEndPoint("EndToStart", rng);
                        i = r2.text.length;
                    }
                } else {
                    if ( rng.startContainer && rng.endContainer 
                   &&  rng.startContainer == rng.endContainer 
                   && rng.startContainer == rng.commonAncestorContainer 
                   && rng.commonAncestorContainer.parentNode == el) {

                        //make sure your DIV does not have any inner element,
                        //otherwise more code is required in order to filter
                        //text nodes and count chars

                        i = rng.startOffset;
                    }
                }
            }
        }
        return i;
    };

})();


$("#content").on('mousedown', function () {
    $("#content").html(content)
});

var content = $("#content").html();

$("#content").on('mouseup', function () {
    var start = getSelectionStart(this);
    var len = getSel().toString().length;


    $(this).html(
            content.substr(0, start) + 
            '<span style=\"background-color: yellow;\">' +
            content.substr(start, len) + 
            '</span>' +
            content.substr(Number(start) + Number(len), content.toString().length));
});

References: http://bytes.com/topic/javascript/answers/153164-return-selectionstart-div

darksky
  • 1,955
  • 16
  • 28