I have this html on the checkout page of my website
<tr class="order-total">
<th>Total</th>
<td><strong><span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">$</span>24,500.00</span></strong>
</td>
</tr>
I have to multiply this price(24,500) with 0.0198 and show it in side a div which is on the same page in another section. This is what I have tried:
jQuery('.woocommerce-Price-amount.amount').load(function () {
var totprice = jQuery('.woocommerce-Price-amount.amount').val();
var leaseprice = totprice * 0.0198;
jQuery(".leaseprice_container").append(leaseprice);
});
The section where I am trying to show the leaseprice has this html:
<div class="leaseprice_container">
<p><strong>Prices are in USD.</strong></p>
</div>
4
Answers
There is no .load on an html element. One would use
$("selector").load("url",function() {...})
to load data from the backend
A span does not have .val()
Try this
jQuery()
function which is shorthand fordocument.load
.text()
of the span because onlyinputs
have values..replace(/[^d.-]/g, '')
)I used a regex to remove all not number characters, then do all the calculation in integer, and then returning to the real decimal base, in order to avoid rounding errors.
You can ge the value using
text()
and notval()
, which is for input elements, you can trim it and replace the$
sign and the comma with an empty character in order to have a correct float number format in order to makeparseFloat()
to work. Then you can place the result in the section you want: