Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pasting into a textarea

Status
Not open for further replies.

Geee

Programmer
Joined
Apr 23, 2001
Messages
253
Location
GB
Hi,

I want to use javascript to paste the contents of a variable into a text area where the current cursor is. Is this possible? I am a real newbie when is comes to javascript and any help will be appreciated!

G

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
To be honest, I only have a textarea and a button at the moment. I would take any advice you can throw.

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
Well, to test your example, you probably need more than one textarea so you can verify that the pasting is going into the textarea with focus!

The thing is, of course, once you hit the button, focus has left the textarea, so you have to have a way of setting some kind of global JavaScript variable flag with the ID of the last textarea to have received focus and send your text there.

If you have this global var:

var focusTextArea;

...and set it using the ONFOCUS event in the TEXTAREA tag as: <textarea onfocus='setThis(this);'>

...then, create the function setThis(obj) that assigns the sent parameter to the global var, then all you need to do is use the button's ONCLICK event to call a function that does:

focusTextArea.value = "This is the text I want to paste."

That should get you started...

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Hi, thanks for your help. I've worked out how to put text into a textarea using TextArea.value = "text", but I don't know how to actually put the text where the cursor is.

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
Geee said:
but I don't know how to actually put the text where the cursor is

If you're triggering this behavior by clicking the button, the cursor isn't anywhere (focus is on the button). This is why I recommended the path I recommended.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
do a google search for "javascript createtextrange" - that will give you a good start

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Ooooh... I misunderstood.

Geee said:
I want to use javascript to paste the contents of a variable into a text area where the current cursor is.

I thought Geee meant the textarea that currently had focus. I see now that what was meant was the specific location of the cursor within the textarea. My bad.

You still have the problem of losing focus when you click the button, but if you use the ONKEYDOWN event in conjunction with textranges (as kaht suggests), you might be able to do something. I have no experience with textranges except to say that people ask about them a lot and from my understanding, they are not cross-browser compatible and might be complicated to implement.

Although it would be a complicated function to write, if you grab the current value of the textarea ONKEYDOWN and compare it to the value ONKEYUP, you can probably figure out where the cursor is, create a new string with the text inserted at that point, then set the value of the textarea with the new string. Unfortunately, since you can change the value of the textarea with the mouse, it can get pretty complicated pretty quickly.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top