最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Change selected text via javascript - Stack Overflow

programmeradmin0浏览0评论
window.addEventListener("keydown", function(e){
/*
keyCode: 8
keyIdentifier: "U+0008"
*/
if(e.keyCode === 16 && getSelectionText() != "") {
    e.preventDefault();
      replaceSelectedText(strcon(getSelectionText()));
    }
});

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

function strcon(givenString) {
    var b = '';
    var a = givenString;
    for (i = 0; i < a.length; i++) {
        if (a.charCodeAt(i) >= 65 && a.charCodeAt(i) <= 90) {
            b = b + a.charAt(i).toLowerCase();
        }
        else
            b = b + a.charAt(i).toUpperCase();
    }
    return b;
}

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

The code I have right now seems to change the appearance of the actual text instead of actually changing it. For example, when I'm on Facebook and I press the certain key, the text seems to have changed but then when I press enter, the text goes back to what it was before.

I believe the issue is with the function replaceSelectedText but I'm not sure how to fix it.

Any ideas?

No JQuery please.

/

window.addEventListener("keydown", function(e){
/*
keyCode: 8
keyIdentifier: "U+0008"
*/
if(e.keyCode === 16 && getSelectionText() != "") {
    e.preventDefault();
      replaceSelectedText(strcon(getSelectionText()));
    }
});

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

function strcon(givenString) {
    var b = '';
    var a = givenString;
    for (i = 0; i < a.length; i++) {
        if (a.charCodeAt(i) >= 65 && a.charCodeAt(i) <= 90) {
            b = b + a.charAt(i).toLowerCase();
        }
        else
            b = b + a.charAt(i).toUpperCase();
    }
    return b;
}

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

The code I have right now seems to change the appearance of the actual text instead of actually changing it. For example, when I'm on Facebook and I press the certain key, the text seems to have changed but then when I press enter, the text goes back to what it was before.

I believe the issue is with the function replaceSelectedText but I'm not sure how to fix it.

Any ideas?

No JQuery please.

https://jsfiddle/1rvz3696/

Share Improve this question edited Apr 28, 2016 at 3:29 Dgameman1 asked Apr 28, 2016 at 3:18 Dgameman1Dgameman1 3785 silver badges24 bronze badges 8
  • can you please create jsfiddle or plunker? – brk Commented Apr 28, 2016 at 3:26
  • @user2181397 Done and done – Dgameman1 Commented Apr 28, 2016 at 3:29
  • 1 I don't understand the problem. I tried your jsfiddle: I entered "hello foobar". I select a word and press shift key. Then the word appears before the input box as capital letters. What of this is undesired behavior? – owler Commented Apr 28, 2016 at 3:33
  • @ILovephp123 I still don't get it.. Your fiddle works fine to me or I really still dont get it – choz Commented Apr 28, 2016 at 3:33
  • @owler, the goal is to replace the selected text, not add it somewhere else – Dgameman1 Commented Apr 28, 2016 at 3:35
 |  Show 3 more ments

1 Answer 1

Reset to default 5

You have to get your textarea element to replace the value in it. This is how your replaceSelectedText function should look like,

function replaceSelectedText(text) {
  var txtArea = document.getElementById('myTextArea');
  if (txtArea.selectionStart != undefined) {
    var startPos = txtArea.selectionStart;
    var endPos = txtArea.selectionEnd;
    selectedText = txtArea.value.substring(startPos, endPos);
    txtArea.value = txtArea.value.slice(0, startPos) + text + txtArea.value.slice(endPos);
  }
}

And here's the Fiddle.

Without specific id, you can replace txtArea to this.

var txtArea = document.activeElement;

And another Fiddle

发布评论

评论列表(0)

  1. 暂无评论