Adding several inputs with same id jquery or Avascript

Asked

Viewed 88 times

0

então galera tenho seguinte codico 
 function calcularFinal() {      
   var contas = document.querySelectorAll("#produ"); 
   var total = 0 ;
  for(var i= 0; i<contas.length; i++){
    var conta = parseFloat(contas[i].value.replace(',','.'));
   var valor = (conta+valor);
  }
  console.log(valor); 
}

she picks up several inputs with even ids on them nothing more has than a float value just wanted to sum them all more n I’m getting until weighed q was because of the comma

1 answer

1


Hello,

I created a jsfiddle to exemplify, but below follows a function that does this.

function calcularFinal(){
  soma = 0;
    numeros = document.querySelectorAll('#produ')
    .forEach((elemento) => {
        soma += Number.parseFloat(elemento.value);
    });
  console.log(soma);
}

An important thing is not to have multiple elements with the same ID, the id must be unique for each element. For this, use classes. In the example below you can see this.

Example

  • vlw friend by tip guess q was that same q you said id should be only obg by tips S2 worked perfectly

  • Please, we’re here to help.

Browser other questions tagged

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