2
I’m wanting to get all the textContent das td that then inside a tr of a table. However, this should happen when I click on your respective parent TR.
- These tr are generated in PHP and returned by AJAX and later inserted into tbody of the HTML table.
Goes below a part of the TR build PHP code. (Only a part of foreach. has more tds. I only put one for example):
<?php
$tabela="";
foreach($lista_ativo as $value)
{
$tabela .= "<tr class='tr-row border'>";
$tabela .= "<td class='pl-3 p-3 td-descr'>$value[descricao]</td>";
$tabela .= "<td class='text-center td-mensal border'>" . number_format($value['m1'], 0, ',', '.') . "</td>";
$tabela .= "<td class='text-center td-mensal border'>" . $value['percent_m1'] . "%</td>";
?>
How the table looks after mounted in HTML:
- Now I’d like every time I click on a TR of this, I’d take each textContent of all your TD and assign it to an array.
Below is a part of how my Javascript is getting, but I’m still not succeeding.
liberarClickTr();
function liberarClickTr() {
//Esse Array
let valores_td = [];
document.querySelectorAll(".tr-row").forEach(e => {
e.addEventListener("click", function(e) {
tr = e.target.closest("tr");
console.log(tr);
});
});
}
- Below is what comes out of that console.log (I think it’s a good sign).
- When I click on the TR already exits the TR element on the console.log. I believe I’m almost on track.
From now on, thank you!
Double-clicking the same line will add duplicate text to the array.
– Sam
Yes, but I’ll reset the array before that.
– Gato de Schrödinger
My intention is to get these textContent with the click on tr. I’m used to doing this with jQuery, but I would like to do this with Pure JS. I’m not a fan of that addeventlistener
– Gato de Schrödinger