Add and subtract values from various input’s after created or removed with appendchild and removeChild.

Asked

Viewed 94 times

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>

1 answer

1


This way you are inserting fields with ids equal (1 or 2).

Gere idsingle s counting the number of fields. And don’t just use a number like id, include a string together. In my example below, I am generating ids: campo0, campo1, campo2 etc...

function adicionar(conteudo) {
  var node = document.createElement("input");
  var novo_id = document.querySelectorAll("#lista input").length;
  node.setAttribute("id", "campo"+novo_id); //insere o id
  node.setAttribute("type", "text"); //insere o tipo
  node.setAttribute("value", conteudo); //insere o valor
  document.getElementById("lista").appendChild(node); //escreve na div
}
<div id="lista"></div>
<button onclick="adicionar('5');">adicionar input 1</button>
<button onclick="adicionar('7');">adicionar imput 2</button>

To add or subtract you can take two or more fields by the respective id. Or if you’re going to use them all, you can select them all at once with document.querySelectorAll("#lista input") and tie a bow for:

function somarTudo(){
   var campos = document.querySelectorAll("#lista input");
   var soma = 0;
   for(var x=0; x<campos.length; x++){
       soma += parseInt(campos[x].value);
   }

   return soma;
}

To get the summed values, just call the function somarTudo().

  • 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)

  • I put in the answer a function to sum it all up.

  • 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

  • Exchange the parseInt for parseFloat.

  • It worked, and to limit the decimal places after the comma I could use the toFixed() ?

  • 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))

  • I did it differently, I’m wearing it like this and it worked: .innerHTML = soma.toFixed(2)

  • Ah yes, tb works this way.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.