Dear: How can I do to insert rows in an html table, where the inserted elements are inputs of type text, and I need that each one has a different id. Using one of the solutions proposed by the user Juan Pinzón (from already grateful). I came to the following:
<!-- <!DOCTYPE html>
<html>
<head>
<script src="jquery-3.3.1.min.js"></script>
<meta charset="utf-8">
<body>
<button type="button" id="agregar">Agregar</button>
<table id="tabla">
<thead>
<tr>
<th scope="col">Nombre</th>
<th scope="col">Apellido</th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</body>
</html>
<script>
$("#agregar").on("click", function(){
$('#tabla > tbody:last-child').append( '<tr> <td><input type="text" name="nombre" id="nombre" ></td><td><td><input type="text" name="apellido" id="apellido" ></td><td>');
});
</script>
Now, as I do so that id="name", the second time I click on "add", go to id="name2", I think I should do a cycle (for or while) where this increase variable, but I can not implement it with this function of jquery, I managed to do something similar with php (in another circumstance), but here it does not work, since when from php I must call it with echo, it starts a coke of single and double quotes impossible to solve . As you may have noticed, I am quite new to the subject. So, if you send me to investigate a function, library, etc, everything is more than good. Thank you very much, and excuse the extensive consultation. Greetings. PS: if for some reason the code is not shown complete (like me in the preview), I tell you that in the body of the html the table is created as table id="table".