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

writing information from a button into a text box

P: 1
hi im literally a complete amateur at the but i was wondering how to write the information from a button e.g. A into a text box. I know it involves an onclick command but i just can figure it out. any help would be greatly appreciated below is what i already have:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Page Title</title>
  4. </head>
  5. <body>
  6. <h1>Table</h1>
  7. <form>
  8.     <table border="1">
  9.  <tr>
  11.       <td><input type='button' value='A' /></td>
  12.      <td><input type='button' value='B' /></td>
  13. </tr>
  14.  </table>
  16.  <input type="text" name="searchbox" size=15>
  17.  <div style="position:absolute; left:150px; top 150px;"></div>
  19. </form>
  20. <hr />
  21. </body>
  22. </html>
Dec 7 '09 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,639
the procedure is quite simple:
write a function, where the value of the button is written to the target elementís value.
pass function to event handler

Expand|Select|Wrap|Line Numbers
  1. // put an id to the elements, so that you can access them easily
  3. // write value to target
  4. // "this" will later refer to the button
  5. function passValue()
  6. {
  7.     document.getElementById("target_input").value = this.value;
  8. }
  10. // register events (Button A, Button B)
  11. document.getElementById("btnA").addEventListener("click", passValue, false);
  12. document.getElementById("btnB").addEventListener("click", passValue, false);
  13. // IEís poor event handling requires a workaround, so google for the addEvent() crossbrowser solution
Dec 7 '09 #2

P: 15
A simpler way could be
Expand|Select|Wrap|Line Numbers
  1. <input type='button' value='A'  onclick="this.form.searchbox.value=this.value;" />
Dec 8 '09 #3

Expert Mod 5K+
P: 8,639
"simple" is a matter of preference. suppose you want to change the target element…
Dec 8 '09 #4

Post your reply

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