Using onClick to paste code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrking
    New Member
    • Feb 2008
    • 28

    Using onClick to paste code

    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:

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function check(browser)
      {
      document.getElementById("answer").value=browser;
      }
    </script>
    
    </head>
    <body>
    
    <form>
    <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/michael-ashton/">Michael</a>' value="Michael"<br />
    <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/dan-durand/">Dan</a>' value="Dan"<br />
    <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/barb-towell/">Barb</a>' value="Barb"<br />
    <input type="button" onclick="check(this.id)" id='<a href="http://bikramhotyoga.ca/index.php/instructors/gulie-ajania/">Gulie</a>' value="Gulie"<br />
    <br /><br />
    The teacher is: <input type="text" id="answer" size="20">
    The teacher is: <input type="text" id="2" size="20">
    </form>
    
    </body>
    </html>
    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

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

    Comment

    • mrking
      New Member
      • Feb 2008
      • 28

      #3
      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?

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Originally posted by mrking
        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.

        Code:
              <script type="text/javascript">
        
              function check(browser, fieldId)  {
                document.getElementById(fieldId).value=browser;
                }
              </script>

        Comment

        • mrking
          New Member
          • Feb 2008
          • 28

          #5
          Originally posted by pronerd
          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.

          Code:
                <script type="text/javascript">
          
                function check(browser, fieldId)  {
                  document.getElementById(fieldId).value=browser;
                  }
                </script>
          I tried this code but with not luck. I placed my cursor in the text box, click one of the buttons and nothing happens.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You will need to keep a reference to currently focused element using the onfocus event handler of each text box.

            Comment

            Working...