473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Replace selected text in a textarea?


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
2 5541
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: R.G. Vervoort | last post by:
Is it possible to change selected text in a textarea without using java and only php or html. There are lots of solutions in a javascript but i would like to try it without. If it is possible to...
11
by: Shaiboy_UK | last post by:
Hi All, Hope someone can help here!! I'm putting a standard mail page together, that will allow my users to sent templete emails out to people. The files are stored in text files. I have...
7
by: Razzbar | last post by:
I have a textarea and some buttons. The buttons apply functions to the text the user selects, i.e. the good old "wrap with a tag" thing we all have seen in 100 different editors. Trouble is,...
8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
4
by: Adnan Siddiqi | last post by:
Hi Pardon me if I am not making any sense.What I want to know the cordinates of selected text by user so that i can save/retrieve them later.is It possible.I am lookig for both IE and Firefox...
17
by: alxasa | last post by:
Hi, can someone please show me how to most elegently do this?..... I have a textbox, and I want to search the contents of it and replace all instances of a certain word, and replace that word...
3
by: FunkHouse9 | last post by:
I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to...
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
1
by: destiny007 | last post by:
can any one help me to write code to capture selected text from a page but problem is that i am not able to decide where to call the functio.in this case the function is called in all cases of mouse...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.