2
Well my problem is this, with the appendchild of Javascript I create an input with X value with its own ID but can be random, and add another with the same method with the same situation of the other but with different value (Taking into account that X is a numerical value), well, my problem is I don’t know how I’m going to add or subtract these input’s by having random id’s as they are created or removed. I am grateful if you can help me. Follow below the code I use to create:
function adicionar(conteudo) {
var node = document.createElement("input");
dados = conteudo.split('|'); //separa a id do valor e cria um array nos dados
node.setAttribute("id", dados[0]); //insere a id
node.setAttribute("type", "text"); //insere o tipo
node.setAttribute("value", dados[1]); //insere o valor
document.getElementById("lista").appendChild(node); //escreve na div
}
<div id="lista"></div>
<!--a esquerda a id a direia o valor-->
<button onclick="adicionar('1|5');">adicionar input 1</button>
<button onclick="adicionar('2|7');">adicionar imput 2</button>
The id’s will generate from php, from the high increment column, there was only one example, so I said it would be random, and yes, I will add something that specifies before more the numeric id, But could it be in another function to use this querySelctorAll just to take all the values and make a sum than the one on the list? (Maybe you can even put a return to this function every time it is created or removed)
– João Victor
I put in the answer a function to sum it all up.
– Sam
There is a small problem, this calculation does not sum with broken values with a point (ex: 2.1), it only takes the number before the comma and sum
– João Victor
Exchange the
parseInt
forparseFloat
.– Sam
It worked, and to limit the decimal places after the comma I could use the
toFixed()
?– João Victor
You can use yes, but you have to use two parseFloat, because toFixed converts the value into string:
parseFloat(parseFloat(campos[x].value).toFixed(2))
– Sam
I did it differently, I’m wearing it like this and it worked:
.innerHTML = soma.toFixed(2)
– João Victor
Ah yes, tb works this way.
– Sam