I have a problem after add new row. When I click 'ADD NEW', the new row successfully added. The problem is, all 4 button at column 'Action' cannot run well. I set button 'view' to go to google.com after clicked but it only successful at the first row. Second row and onwards are fail. Below is my code.
$("#add_new").click(function () {
$("#maintable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td style="padding:5px;">' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
document.getElementById("myButton").onclick = function () {
location.href = "dashboard.php";
};
<table id="maintable" width="100%" cellpadding="0" cellspacing="0"
class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
<th align="center">Action</th>
</tr>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;" width="20%">
<input type="text" name="rollno<? $i ?>" />
</td>
<td style="padding:5px;" width="20%">
<input type="text" name="firstname<? $i ?>" />
</td>
<td style="padding:5px;" width="20%">
<input type="text" name="lastname<? $i ?>" />
</td>
<td style="padding:5px;" width="40%">
<button type="button" id="myButton">View</button>
<button type="button">Edit</button>
<button type="button">Delete</button>
</td>
</div>
</tr>
</table>
<br><button id="add_new">ADD NEW
</button>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
Can I know what's the problem?