Connecting Tech Pros Worldwide Forums | Help | Site Map

Replace selected text in a textarea?

King of the R.O.U.S.'s
Guest
 
Posts: n/a
#1: Jul 9 '08

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?

Thanks

~Inigo

Gregor Kofler
Guest
 
Posts: n/a
#2: Jul 9 '08

re: Replace selected text in a textarea?


King of the R.O.U.S.'s meinte:
Quote:
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
King of the R.O.U.S.'s
Guest
 
Posts: n/a
#3: Jul 20 '08

re: Replace selected text in a textarea?


Gregor Kofler wrote:
Quote:
Quote:
>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.
>
You need to do some feature testing. Then it boils down to this (untested):
>
MS:
var ieSelection = document.selection.createRange();
ieSelection.text = yourText;
not bothered with InternetExplorer.
Quote:
Gecko and others:
>
var s = yourFormElement.selectionStart;
var s = yourFormElement.selectionEnd;
yourFormElement.value = yourFormElement.value.substr(0, s) + yourText +
yourFormElement.value.substr(e);
Not quite sure how to work with this, it doesn't seem to do anything
when I add it to my code. As I said I'm /VERY/ new to javaScript LOL

The lifted (and edited) text is stored in a var called nw, I assume I
just put that in place of yourText in the above code.

I'm running it through FireFox BTW.

What am I doing wrong?

~Inigo
Closed Thread


Similar JavaScript / Ajax / DHTML bytes