Change span value with jQuery

Asked

Viewed 1,539 times

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 a way to modify each value by traversing the span and adding an extra percentage in each value. this as jQuery code. Can anyone help me with this ? These are just examples.

Thank you !

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>


<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 cannot create multiple spans ids, the span class is one for all products. Example, I would like to give a discount of 10 real in all spans with this class... wanted a function that did this, but I do not know how to do.

  • You need for everyone or just a randomly chosen value?

  • I don’t think you understand, friend... imagine this, I have a product sales website. Each product has its price, the price of the products vary, but are placed in the same span class. What I want to do is take the value that is in the span class (i.e., the value of the product), and give a percentage discount. ie if it is with value of 256,00 the value will change to 230,40. if I give 10% discount.

  • I edited the answer, I think it is this, just go through the elements with each and perform the calculation of each item, an improvement in your question with these details that Voce reported here, why there I understood something randomly, until why Voce used this word, if you had said go through the span elements and update the value of each one would have been much more effective this response.

  • Thank you very much friend, it worked, but I found a problem, because when the value is in thousand, a problem occurs.

  • Friend another doubt already taking advantage of your good will rsrs, if in case came R$ in front of each value, how could I calculate ?

  • Come on, the focus of the answer is exactly how Voce can change the value of spans as title, but I’m going to indicate a path to follow, you need to remove the money mask before you do the calculation, have several plugins and codes for this, after you do the calculation back to mask, as to put R$ in the amount all right, as long as you remember to remove it to calculate...

  • Could you help me with this code that removes the mask for the calculation ?

  • http://blog.fmansano.com/javascript/converter-numero-para-moeda-e-versa/ this article explains how...

Show 4 more comments

Browser other questions tagged

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