1
I’m having a problem calculating and displaying the price in R$ per person in a table.
In the HTML below, we have the number of people and a table, in this table we have the plan price and a blank field <p class="pax-number"></p>
where you should display the price per person.
It seemed simple until then, I used every <p class="preco-plano"></p>
to make a loop and turn the values into numbers, calculate with the number of people and change the text, but it’s not so simple... See the example below:
$(document).ready( ()=> {
//seleciona a quantidade de pessoas e transforma em um numero inteiro
let paxNumber = parseInt(($(".pessoas .field-title").text()).split(" ")[0]);
$('.preco-plano').each( ()=> {
//seleciona o preço do plano e transforma em um numero inteiro
let priceNumber = parseInt($(this).text().split('$')[1]);
//calcula o preço por pessoa
let pricePerPerson = priceNumber / paxNumber;
//seleciona o campo onde deve ser exibido o valor por pessoa e escreve
$(this).closest('p').find('.pax-number').html('R$' + pricePerPerson + 'por pessoa');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="pessoas">
<span class="field-title">4 pessoas</span>
</div>
<table class="tabela-resultados">
<tr class="">
<td class="bloco-preco" style="height: 180px;">
<p class="nome-plano">Plano Tal</p>
<span>
<p class="desconto">R$555,00</p>
<p class="preco-plano">R$316</p>
<p class="pax-number" style="color: green; font-size: 10px;"></p>
</span>
</td>
</tr>
<tr class="">
<td class="bloco-preco" style="height: 180px;">
<p class="nome-plano">Plano XYZ</p>
<span>
<p class="desconto">R$555,00</p>
<p class="preco-plano">R$420</p>
<p class="pax-number" style="color: green; font-size: 10px;"></p>
</span>
</td>
</tr>
<tr class="">
<td class="bloco-preco" style="height: 180px;">
<p class="nome-plano">Plano Cucurioso</p>
<span>
<p class="desconto">R$555,00</p>
<p class="preco-plano">R$188</p>
<p class="pax-number" style="color: green; font-size: 10px;"></p>
</span>
</td>
</tr>
</table>
<script src="jquery-3.2.1.min.js"></script>
I believe the problem is when I select the value and turn it into an integer number let paxNumber = parseInt(($(".pessoas .field-title").text()).split(" ")[0]);
because he doesn’t give me a number, if I have to use the dial $('.preco-plano')
instead of $(this)
It returns me a Nan because it takes not only the value of that block, but of all the blocks in the table.
I tried to do something similar using the for
but unsuccessfully too.
Someone could shed some light on what I’m doing wrong?
Thank you in advance!
sincerely! just have to thank! I had come to read about . each no w3School and I did a test using "element", but with the wrong syntax. Thank you so much for showing me where I went wrong. So whenever I use the Arrow Function and need the . each, I use the "element" to select... and the Closest, should I always select the correct parent object? my thanks very much! tmj !
– MSHijo
That’s right. Arrow does not reference $(this) in function.
– Sam