I need some assistance with extracting and calculation numeric values from the the strings based on id assigned to span text tags. For example I need to calculate 5% discount and display correct amount in another field.
<span id="discount">5% off<span>
<span id="price">£2.75</span>/per month</span>
$( document ).ready(function() { {
var discount= "#discount"; // a string
discount= discount.replace(/\D/g, ''); // a string of only digits, or the empty string
var discount= parseInt(discount, 10); // now it's a numeric value
var price= "#price"; // a string
price= price.replace(/\D/g, ''); // a string of only digits, or the empty string
var discount= parseInt(price, 10); // now it's a numeric value
var discountValue = (discount * price) / 100;
var newPrice= price + discount;
$("#price").text(newPrice);
})
I am not an experet of Jquery and JavaScript thus please understand my limited knowledge and I do appreciate if someone could explain how to achieve expected solution. Thanks
{on$( document ).ready(function() { {.$("#discount").html();to get the value in your span, and the same thing for the discount value.price = price.replace('£', '')instead ofdiscount = price.replace('/\D/g', '')because that regex is removing the decimal point, and you are also assigning the wrong variable there.