View Full Version : Javascript textbox population

05-03-2007, 02:22 PM
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:

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:

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 :)

05-03-2007, 07:39 PM
Do you also have six text boxes or just one? This works with one box and one button's value:

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