You need to be logged in to post in the forums. If you do not have an account, please sign up first.
Text selection copy from a textarea
I tried to select and copy text inside a textarea of a HTML document in Opera 10.5, using the method document.getSelection(). However, it does not select any text which is inside the textarea, although the command works when one selects text in the rest of the document. I tried to do the same thing with document.selection.createRange(), but it did not work either. On the other hand, these methods work well in IE and in the Mozilla family.I wonder which adjustment should be made to the expression (or to the document?) in order to copy the selection. If anyone can give me advice on this, thanks in advance.
15. April 2010, 09:54:10 (edited)
From google-translate.js:
Also, you can simply go through all textareas:
var selText = function(w){var t; return w ? w.document.getSelection() || (t = w.navigator.lastClick.textArea) && t.value.substring(t.selectionStart, t.selectionEnd) : ''};
Also, you can simply go through all textareas:
var getTextInTextarea=function(){
var tArea = document.getElementsByTagName('textarea');
for(var i = tArea.length; i--;){
var t = tArea[i];
var txt = t.value.substring(t.selectionStart, t.selectionEnd);
if(txt)return txt;
}
};
Thanks a lot, Russian Federation for your help. It works now! I only had to make some small changes. The final form, that works with my browser version, is this one:
function getTextInTextarea()
{
var tArea = document.getElementById('textarea');
var txt = (tArea.value).substring(tArea.selectionStart, tArea.selectionEnd);
//alert(tArea.value); // (To check the textarea value. It works correctly.)
//alert(txt); // (To check the selection value. It works correctly.)
return txt;
}
One may notice that the navigation through the content is no longer needed. Maybe these differences are due to the browser version.
function getTextInTextarea()
{
var tArea = document.getElementById('textarea');
var txt = (tArea.value).substring(tArea.selectionStart, tArea.selectionEnd);
//alert(tArea.value); // (To check the textarea value. It works correctly.)
//alert(txt); // (To check the selection value. It works correctly.)
return txt;
}
One may notice that the navigation through the content is no longer needed. Maybe these differences are due to the browser version.