0
I have a table with dates on which I am mounting a button that adds seven days to the present value. As there are several lines, the intention is that each date has its own postponement button, however I can’t get the current date of the line where the button responsible for changing it is.
In short, I tried to use filterItem[i]
to get the date of each line, but it doesn’t work. In the code below, I left only the third row selects (which gives me an indication that the repeat loop is not working):
Where am I going wrong?
var filterItem = document.getElementById('table').querySelectorAll('td:nth-child(2)')
var adiarEntrega = document.querySelectorAll('.adiar')
for (var i = 0; i < filterItem.length; i++) {
var dataAtual = filterItem[i].innerHTML;
var botao = adiarEntrega[i];
botao.addEventListener('click', function() {
var datainicial = filterItem[2].innerHTML;
var dias = parseInt(7);
var partes = datainicial.split("/");
var ano = partes[2];
var mes = partes[1]-1;
var dia = partes[0];
datainicial = new Date(ano,mes,dia);
datafinal = new Date(datainicial);
datafinal.setDate(datafinal.getDate() + dias);
var dd = ("0" + datafinal.getDate()).slice(-2);
var mm = ("0" + (datafinal.getMonth()+1)).slice(-2);
var y = datafinal.getFullYear();
var dataformatada = dd + '/' + mm + '/' + y;
filterItem[2].innerHTML = dataformatada
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<table id="table">
<tr>
<th>Coluna 1</th>
<th>Coluna 2</th>
</tr>
<tr>
<td class="adiar">Avançar 7 dias --> </td>
<td>12/03/2018</td>
</tr>
<tr>
<td class="adiar">Avançar 7 dias --> </td>
<td>15/06/1962</td>
</tr>
<tr>
<td class="adiar">Avançar 7 dias --> </td>
<td>23/11/2009</td>
</tr>
</table>
That’s some dough, Iglan! So just use it
let
instead ofvar
? Thank you very much for the return and for the clear explanation, my dear!– winiercape
Exactly! Don’t use anymore
var
, onlylet
andconst
to declare variables.– Cmte Cardeal
Thanks for the tip! =]
– winiercape