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

Using onClick to paste code

P: 28
Hi,

I am working on having a button click and then it will paste code into a selected box.

I am having a little bit of an issue though.

First my code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript">
  5. function check(browser)
  6.   {
  7.   document.getElementById("answer").value=browser;
  8.   }
  9. </script>
  10.  
  11. </head>
  12. <body>
  13.  
  14. <form>
  15. <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/michael-ashton/">Michael</a>' value="Michael"<br />
  16. <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/dan-durand/">Dan</a>' value="Dan"<br />
  17. <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/barb-towell/">Barb</a>' value="Barb"<br />
  18. <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/gulie-ajania/">Gulie</a>' value="Gulie"<br />
  19. <br /><br />
  20. The teacher is: <input type="text" id="answer" size="20">
  21. The teacher is: <input type="text" id="2" size="20">
  22. </form>
  23.  
  24. </body>
  25. </html>
  26.  
  27.  
Two things:

1) It will only paste into the box that has an id of 'answer' instead of a selected box.


Can anyone help me with this?

Thanks!!

Cheers,

Michael
Apr 20 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
What do you mean by selected box? Do you mean the one which has focus?

Note that you haven't closed your input buttons properly.
Apr 20 '08 #2

P: 28
Oops, thanks.

I have multiple text boxes on one page and would like to place my cursor in a box, click the button and the text value will paste into it. Click on another text box, click, paste. Etc.

Right now it will only paste into a text box with an id of 'answer'

Is there a way to have it paste in whatever box I select?
Apr 21 '08 #3

Expert 100+
P: 392
Is there a way to have it paste in whatever box I select?
It is only selecting the one with the value of 'answer', because you hard coded the string 'answer' as the ID value to pull. So pass the ID of the field you want to use, and then pull that element.

Expand|Select|Wrap|Line Numbers
  1.       <script type="text/javascript">
  2.  
  3.       function check(browser, fieldId)  {
  4.         document.getElementById(fieldId).value=browser;
  5.         }
  6.       </script>
  7.  
Apr 21 '08 #4

P: 28
It is only selecting the one with the value of 'answer', because you hard coded the string 'answer' as the ID value to pull. So pass the ID of the field you want to use, and then pull that element.

Expand|Select|Wrap|Line Numbers
  1.       <script type="text/javascript">
  2.  
  3.       function check(browser, fieldId)  {
  4.         document.getElementById(fieldId).value=browser;
  5.         }
  6.       </script>
  7.  
I tried this code but with not luck. I placed my cursor in the text box, click one of the buttons and nothing happens.
Apr 23 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You will need to keep a reference to currently focused element using the onfocus event handler of each text box.
Apr 23 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.