Copy text into text box via Javascript when button clicked

Slobrob

Born

Posts: 2

3+ Months Ago

Hi There,

I'm a bit of a newbie but im doing a little project for myself and have started to learn javascript.What I am after is to create buttons so that each time a button is clicked it is copied and pasted into the text box below. I've to do this but i have seem to have an issue - I have several buttons and everytime i press a different button it comes up with the same information on the first button.

for example, i have button 1, 2, 3, 4 etc. and if i press button 1 or button 2 or 3 it will still paste button 1. Any ideas? Ive attached my code below

I guess you tested this in IE? What you should note (and be aware of) is that neither your or mine example seems to be cross browser compatible - I have a feeling you will note why.

Slobrob

Born

Posts: 2

3+ Months Ago

Great! thanks for that WritingBadCode, I thought i might have been along the right lines just wasnt sure where i was going wrong. Yes I am aware that it only works in IE - which is the only web browser i will be using for this project.

I have a couple of questions though. Firstly, whenever I click one of the buttons it copies itself into the text box below but if I accidently click into the whitespace it resets itself and any buttons I press after clicking in the whitespace supersedes the buttons ive already clicked. is there a way to get around this so that it doesnt happen?

my second question is how would i go about adding code that makes an undo button deleting the last line of information?

Cheers

WritingBadCode

Graduate

Posts: 214

Loc: Sweden

3+ Months Ago

Slobrob wrote:

I have a couple of questions though. Firstly, whenever I click one of the buttons it copies itself into the text box below but if I accidently click into the whitespace it resets itself and any buttons I press after clicking in the whitespace supersedes the buttons ive already clicked. is there a way to get around this so that it doesnt happen?

If I understand correctly the issue is the users ability to click in the text field and by doing so the pasted text can end up removing "things".

The user can now click all they want and the text will still end up on the bottom of the textarea. However the code may end up being slow if the amount of data gets big and it will paste text in a way that may or may not be wanted. The better option would be adding a new "node object" eg create a new paragraf or div containing the text. I guess this was not the code you wanted, anyhow:

Slobrob wrote:

my second question is how would i go about adding code that makes an undo button deleting the last line of information?

You have several options one being using string manipulation and keeping a history on any changes and use some cut and/or paste technique. Searching through text however (especially if the amount gets big) is probably less effective than creating new objects and simply selecting and deleting the latest addition from the DOM three. Also nodes should offer more flexibility in general.