By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Replace selected text in a textarea?

P: n/a

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
Jul 9 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 9 '08 #2

P: n/a
Gregor Kofler wrote:
>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.
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
Jul 20 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.