Simple question - is there any way to select a sub-set of the text displayed in a <textarea> control using Javascript?
e.g. have a function like
selectText(startCharNo, endCharNo, textareaName);
It also needs to be IE6 patible.
Simple question - is there any way to select a sub-set of the text displayed in a <textarea> control using Javascript?
e.g. have a function like
selectText(startCharNo, endCharNo, textareaName);
It also needs to be IE6 patible.
Share Improve this question asked Apr 29, 2009 at 7:53 Richard NicholsRichard Nichols 1,94018 silver badges19 bronze badges3 Answers
Reset to default 6yes, it is possible
element.focus();
if(element.setSelectionRange)
element.setSelectionRange(startCharNo, endCharNo);
else {
var r = element.createTextRange();
r.collapse(true);
r.moveEnd('character', endCharNo);
r.moveStart('character', startCharNo);
r.select();
}
element is the reference to the textarea
createTextRange()
http://www.developerfusion./forum/thread/48987/
selectText(startCharNo, endCharNo, textAreaName){
var content = document.getElementById(textAreaName).innerHTML; //value may work too
var piece = content.subString(startCharNo, endCharNo);
return piece;
}