Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to add button to next row in a table

I am trying to get an add button to appear on the next row of each row when the add button is clicked. currently I am getting an empty textfield in the cell where I would like the add button to appear. what would i have to change in my code to get the add button instead of the text field when the add button is clicked?

For the add button's JavaScript I currently have:

     // button cell
  var cellRightSel = row.insertCell(3);
  var bt = document.createElement('input');
  bt.name = 'addBt' + iteration;
  bt.id = 'addBt' + iteration; 
  cellRightSel.appendChild(bt);

and the html for the page is:

<table border="1" id="tblSample">
<tr>
<th colspan="3">Sample table</th>
</tr>
<tr>

<td><input type="text" name="txtRow1"
 id="txtRow1" size="40"  /></td>



<td><input type="text" name="txtRow2"
 id="txtRow3" size="40"  /></td>



<td>
<select name="selRow0">
<option value="value0">text zero</option>
<option value="value1">text one</option>
</select>
</td>


<td><input type="button" value="Add" onclick="addRowToTable();" name="addBt0" /></td>


</tr>
</table>
like image 586
will Avatar asked Apr 23 '26 16:04

will


1 Answers

bt.type = "button"

You forgot to make it a button.

Alternatively you could do

var bt = document.createElement('button');

to make a <button> instead.

like image 196
Raynos Avatar answered Apr 26 '26 06:04

Raynos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!