Tahsin Ahmed
Tahsin Ahmed

Reputation: 3

How to calculate data by using jquery?

I am writing an application. Here is some source code:

    <div class="grid_3 alpha omega selected cheapest">
        <div class="cheapest-body">

            <p class="latest-type">Red Hotel<br />from <span class="latest-offer-price">$96</span>
        </div>
    </div>


    <div class="grid_3 alpha omega selected cheapest">
        <div class="cheapest-body">

            <p class="latest-type">Orange apartment<br />from <span class="latest-offer-price">$64</span>
        </div>
    </div>



    <div class="grid_3 alpha omega selected cheapest">
        <div class="cheapest-body">

            <p class="latest-type">Silver apartment<br />from <span class="latest-offer-price">$71</span></p>

        </div>
    </div>

I want that to write a jquery code which will change each prices(multiple 3 time) that means $71 will show $213, $64 will show $192 and $96 will show $288 when I inject the code from developer console.

I am trying to write this code as like:

temp = $("span.latest-offer-price").text().split('$'); But I failed. Please help me about this issue.

Upvotes: 0

Views: 90

Answers (4)

HM Tanbir
HM Tanbir

Reputation: 1020

Try this:

 function calculate(){ $('.latest-offer-price').each(function(){
      var data = $(this).text().split(1);
      $(this).text('$'+(data*3));
    });
    }
    calculate();

Upvotes: 0

Mamun
Mamun

Reputation: 68933

Try the following:

$('.latest-offer-price').each(function(el){
  var data = $(this).text().substring(1); //taking only the number part
  $(this).text('$'+(data*3)); // multiply the number then prefixing the result with $
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">
        <p class="latest-type">Red Hotel<br />from <span class="latest-offer-price">$96</span>
    </div>
</div>

<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">
        <p class="latest-type">Orange apartment<br />from <span class="latest-offer-price">$64</span>
    </div>
</div>

<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">
        <p class="latest-type">Silver apartment<br />from <span class="latest-offer-price">$71</span></p>
    </div>
</div>

Upvotes: 1

Manuel Perez Heredia
Manuel Perez Heredia

Reputation: 2357

You modify your code to this:

<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">

        <p class="latest-type">Red Hotel<br />from $<span class="latest-offer-price">96</span>
    </div>
</div>


<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">

        <p class="latest-type">Orange apartment<br />from $<span class="latest-offer-price">64</span>
    </div>
</div>



<div class="grid_3 alpha omega selected cheapest">
    <div class="cheapest-body">

        <p class="latest-type">Silver apartment<br />from $<span class="latest-offer-price">71</span></p>

    </div>
</div>

and then

temp = $("span.latest-offer-price").text();

let result = parseInt(temp) * 3;

or

let result = parseFloat(temp) * 3.0;

Upvotes: 2

maddy23285
maddy23285

Reputation: 786

Try this:

$("span.latest-offer-price").each(function(){
var val = parseFloat($(this).text().split('$')[1])*3;
$(this).text("$"+val);
});

Upvotes: 0

Related Questions