adrianjohnson
Programmer
Ok, I hope I explain this properly.
I have a form, with a table inside. The table has a header row, and 1 row where the user can insert data. If the user needs another row, they click a button and a row is added using javascript.
However, the row itself has a combo box, and I'd like the combo box to be populated with "Yes" and "No" when the new row is created.
Part of the javascript to add the row is ...
... and the HTML code is ...
The code I've removed from the addRowToTable function is simply the bits to add the other fields, but as they're all text boxes I thought I'd remove the duplicated code.
So, has anyone got any ideas. I'm nearly there, I think, so I just need this little bit.
Thanks,
Adrian Johnson
I have a form, with a table inside. The table has a header row, and 1 row where the user can insert data. If the user needs another row, they click a button and a row is added using javascript.
However, the row itself has a combo box, and I'd like the combo box to be populated with "Yes" and "No" when the new row is created.
Part of the javascript to add the row is ...
Code:
function addRowToTable()
{
var tbl = document.getElementById('tblInvoice');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// Loop through and add the stuff to the cells.
var cellQTY = row.insertCell(0);
var el = document.createElement('input');
el.setAttribute('type', 'text');
el.setAttribute('name', 'tQTY' + iteration);
el.setAttribute('class', 'TinyTextBox');
cellQTY.appendChild(el);
var cellVAT = row.insertCell(1);
var el = document.createElement('select');
el.setAttribute('size', '1');
el.setAttribute('name', 'cmbVAT' + iteration);
el.setAttribute('class', 'ComboBox');
el.setAttribute('option', 'Yes');
el.setAttribute('option', 'No');
cellVAT.appendChild(el);
}
... and the HTML code is ...
Code:
<table border="0" width="95%" id="tblInvoice">
<tr>
<td valign="top" width="100">Qty:</td>
<td valign="top">Description:</td>
<td valign="top">Unit Price:</td>
<td valign="top" width="100">Apply VAT?:</td>
<td align="center" width="100"><input type="button" value="+" name="btnAddRow" class="Button" onclick="addRowToTable();"></td>
</tr>
<tr>
<td valign="top" width="100"><input type="text" name="tQTY1" class="TinyTextBox"></td>
<td valign="top"><input type="text" name="tDesc1" class="TextBox"></td>
<td valign="top"><input type="text" name="tPrice1" class="SmallTextBox"></td>
<td valign="top" width="100">
<select size="1" name="cmbVAT" class="ComboBox">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td valign="top" width="100"> </td>
</tr>
</table>
The code I've removed from the addRowToTable function is simply the bits to add the other fields, but as they're all text boxes I thought I'd remove the duplicated code.
So, has anyone got any ideas. I'm nearly there, I think, so I just need this little bit.
Thanks,
Adrian Johnson