-2

I need to make a pseudo-chat addition to my website. The idea is that you write somewhere on the website, then press a button next to it and it transfers the written to a frame above. I tried doing it with textarea and even found a code how to select and copy the text, but it was also said that it works only in IE. Does anyone have an alternative idea, because textarea seems a little iffy : /

Thanks in advance

nils
  • 335
  • 3
  • 5
  • 15

1 Answers1

0

If you want to copy text from a textarea reliably and only need to store it in a variable rather than to the user's clipboard (which seems to be what you're suggesting), the following will do it in all major browsers:

function getSelectedText(textarea) {
    if (typeof textarea.selectionStart == "number") {
        return textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
    } else if (typeof document.selection != "undefined") {
        textarea.focus();
        return document.selection.createRange().text;
    }
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536