writing information from a button into a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NathB07
    New Member
    • Dec 2009
    • 1

    writing information from a button into a text box

    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:

    Code:
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>
    <h1>Table</h1>
    <form>
        <table border="1">
     <tr>
        
          <td><input type='button' value='A' /></td>
         <td><input type='button' value='B' /></td>
    </tr>
     </table>
    
     <input type="text" name="searchbox" size=15>
     <div style="position:absolute; left:150px; top 150px;"></div>
    
    </form>
    <hr />
    </body>
    </html>
    Last edited by Dormilich; Dec 7 '09, 11:48 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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

    Code:
    // put an id to the elements, so that you can access them easily
    
    // write value to target
    // "this" will later refer to the button
    function passValue()
    {
        document.getElementById("target_input").value = this.value;
    }
    
    // register events (Button A, Button B)
    document.getElementById("btnA").addEventListener("click", passValue, false);
    document.getElementById("btnB").addEventListener("click", passValue, false);
    // IE’s poor event handling requires a workaround, so google for the [I]addEvent()[/I] crossbrowser solution

    Comment

    • DMsy2
      New Member
      • Dec 2009
      • 15

      #3
      A simpler way could be
      Code:
      <input type='button' value='A'  onclick="this.form.searchbox.value=this.value;" />

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        "simple" is a matter of preference. suppose you want to change the target element…

        Comment

        Working...