King of the R.O.U.S.'s meinte:
Hi All
How do I replace selected text in a textarea with JavaScript?
I have a text area that the user can select what they want then press
a button that will pick up the selected text, make alterations
(according to rules specified elsewhere) then replace the original
selection with the new text.
I've got everything working great (displays exactly what I want in a
temp prompt) apart from putting the text back, How do I do this?
I'm very new to JavaScript and no doubt missing something very
obvious, can anybody help me sort this out?
You need to do some feature testing. Then it boils down to this (untested):
MS:
var ieSelection = document.selection.createRange();
ieSelection.text = yourText;
Gecko and others:
var s = yourFormElement.selectionStart;
var s = yourFormElement.selectionEnd;
yourFormElement.value = yourFormElement.value.substr(0, s) + yourText +
yourFormElement.value.substr(e);
Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum