search and replace the selected text


  • administrators

    function getSelectedText() {
        if (window.getSelection) {
            txt = window.getSelection();
        } else if (window.document.getSelection) {
            txt =window.document.getSelection();
        } else if (window.document.selection) {
            txt = window.document.selection.createRange().text;
        }
      console.log("selected", txt + "|||")
      if(txt.toString() !== "")
        replaceSelectedText ("hello")
        // return txt;  
    }
    
    function replaceSelectedText(replacementText) {
        var sel, range;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.rangeCount) {
                range = sel.getRangeAt(0);
                range.deleteContents();
                range.insertNode(document.createTextNode(replacementText));
            }
        } else if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            range.text = replacementText;
        }
    }