2
Hello folks I am trying to make a subtraction in the fields Need - Balance and the result in Requested, from the table below, is only working the first item, I tried to move to an array and pick position by position in Javascript but it didn’t work either and as I am using Datatables I have no idea how these ids are being generated by the plugin, I have already made an element inspect and all ids have the same name, can someone explain me how to send these values in sequence to Javascript.
The table rows are generated through query in the database, and the line code is like this:
<tbody>
<?php while($dados = mysqli_fetch_assoc($qry2)){ ?>
<tr>
<td>
<input type="checkbox" name="selecionar[]" value="<?php echo $dados['codproduto'];?>" readonly>
</td>
<td>
<input type="text" maxlength="4" name="codigo[]" id="codigo" value="<?php echo $dados['codproduto'];?>" onkeyup="somenteNumeros(this)"/>
</td>
<td id="descricao">
<?php echo $dados['descricao'];?>
</td>
<td>
<input type="number" maxlength="4" name="necessidade[]" id="necessidade" value="0" onchange="verifica(this)"/>
</td>
<td>
<input type="number" maxlength="4" name="saldo[]" id="saldo" value="0" onchange="verifica(this)"/>
</td>
<td>
<input type="number" maxlength="4" name="solicitado[]" id="solicitado" value="0" onchange="verifica(this)" onfocus="calcularvalor();"/>
</td>
</tr>
<?php } ?>
</tbody>
And in javascript
function calcularvalor() {
var n1 = document.getElementById("necessidade").value;
var n2 = document.getElementById("saldo").value;
var solicitado = n1 - n2;
//var n1 = parseInt(document.getElementById('n1').value, 10);
//var n2 = parseInt(document.getElementById('n2').value, 10);
document.getElementById('solicitado').value = solicitado;
}
Hello friend, I don’t know much about Datatables, but I believe that you should not directly manipulate Ids, but use the Datatables API for this. Check this out, see if it helps: https://datatables.net/reference/api/rows(). date()
– Gleidson Henrique
Whoa, thanks for the tip man, I took a look and it seems that this way also to solve the problem, I’ll see if I can do using this method.
– danielcarlos
Wonderful. It’s the right way. Always prefer to use what’s in the documentation rather than manipulate DOM directly, since if you change the structure of how the data will be shown, the information in the API will hardly change (just because you changed the structure, you know?). That is, it is less laborious to read the documentation and do it the right way than to do it. haha
– Gleidson Henrique