Results 1 to 2 of 2

Thread: Javascript textbox population

  1. #1
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Javascript textbox population

    So I have 6 buttons and when they are clicked I need a textbox to be populated with each of the buttons values.

    Here is the code I have so far:
    Code:
    for(var i = 1; i<=6; i++) {
         var med = document.getElementById('med'+i).innerHTML;
         document.getElementById('TextBox1').value = med;
    }
    I should mention that this is all encapsulated within a function that is called when the user clicks on a button. And obviously the textbox does populate but not with the current button's value, unless you click on the last button. And these "buttons" I refer to are spans with onclick hence why I'm using innerHTML.

    In Flash I would do it like this:
    Code:
    for(var i = 1; i<=6; i++) {
         var med = document.getElementById('med'+i);
         med.ih = med.innerHTML;
         document.getElementById('TextBox1').value = this.ih;
    }
    But the textbox populates with "undefined."

    Thanks again for everybody's help

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Do you also have six text boxes or just one? This works with one box and one button's value:

    HTML Code:
    <input type="text" id="theBox"><br>
    <input type="button" value="Hi!" onclick="document.getElementById('theBox').value=this.value;">
    Several button's could feed the same box in this manner. If you've something else in mind, please clarify and show us the markup that will be populated.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •