Change span value with jQuery

0

I have the following elements:

<span class="teste">256,00</span>
<span class="teste">110,00</span>
<span class="teste">16,00</span>
<span class="teste">26,00</span>

I would like to know how to modify each value by spanning the span and adding an extra percentage for each value. this as jQuery code. Can someone help me with this? These are just examples.

Thank you!

    
asked by anonymous 04.06.2016 / 03:08

1 answer

0

You can loop your jQuery object like this:

Example:

$(".teste").each(function() {
  var valorAtual = this.innerHTML;
  var valorFinal = calculaPorcentagem(valorAtual, 10); // Faz o calculo aqui. 
  this.innerHTML = valorFinal; // Atualiza o valor calculado.
});

function calculaPorcentagem(valor, porcentagem) {
  valor = parseFloat(valor.replace(",", "."));
  valor -= (valor / porcentagem);
  valor = valor.toFixed(2).toString().replace(".", ",");
  return valor;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><spanclass="teste">256,00</span>
<span class="teste">110,00</span>
<span class="teste">16,00</span>
<span class="teste">26,00</span>
    
04.06.2016 / 03:35