I have a tag that has a price in it (ie. $150.00). I want to use jQuery to pull only the text values without the dollar sign ($).
<div>
$150.00
</div>
I want to output the above as 150.00
You can use replace
to replace the $
character with an empty string:
var price = $("div").text().replace("$", "");
Note that in the case of your exact example (with that spacing) the blank spaces will not be removed. If you're going on to use the string as a number (via parseFloat
for example) that won't matter, but if you're wanting to use it as text somewhere else, you may want to remove white space with jQuery's .trim
.
Update - based on comments
replace
returns a String. Once you have that string, you can parse it into a Number with parseFloat
(or parseInt
, but you're working with floating point numbers):
var convertedToNumber = parseFloat(price);
Now that you've got a number, you can perform mathematical operations on it:
var percentage = convertedToNumber * 0.95;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With