1
I have a table. In each row there is a button. At the time the user clicks, I need to get all the data of this line. It is a common table, where data is inserted dynamically, along with the button.
This button has an onclick event that calls a function. I have tried the following commands (unsuccessfully)
function test() { // tblInfracoes
console.log($(this).closest('tr'))
}
returning
w.fn.init [prevObject: w.fn.init(1)]
length: 0
prevObject: w.fn.init [Window]
__proto__: Object(0)
function test() {
var tableData = $(this).children("td").map(function() {
return $(this).text();
}).get();
console.log(tableData);
}
which returns an empty vector
HTML:
<table class="table table-striped" id="tbl-test">
<thead>
<tr>
<th scope="col">nome</th>
<th scope="col">idade</th>
<th scope="col">Período de atividade</th>
<th scope="col">Período de exclusão</th>
<th scope="col">Opções</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Function populating table: (I want you to copy the data when you click Edit)
function insertRow(nome, idade, periodoValido, periodoInvalido) {
var html = `
<tr>
<td>${nome}</td>
<td>${idade}</td>
<td>${periodoValido}</td>
<td>${periodoInvalido}</td>
<td>
<span onclick="test()">
<i class="fas fa-edit"></i>
</span>
</td>
</tr>
`;
$("#tblInfracoes").append(html)
}
Can you put the HTML code of your table? At least a couple of lines is enough.
– Victor Carnaval
function test() {var trParent = $(this).parents("tr")[0]; console.log(trParent)}
– edson alves