How to ignore a specific character in a text element using jquery?

I am trying to get a text element from divthat has this content:<div class="balance"...>$500.48</div>

I need to get 500.48as a value, not as a string.

I used alert($(".balance").text())to check if it returns content and what it does, but I need it to be a number. I know that if I save the string in a variable and do this:

x =  $(".balance").text() ;
x = +x;

It converts the string to a number, so I tried to ignore it $, but I was not successful. What would be the best way to do this?

+4
source share
2 answers

Try

var val = parseFloat($(".balance").text().replace('$',''));


. parseFloat ()
+3
source

, .

var balanceValueElementValue1 = parseFloat($('.balance').text().replace(/[^0-9\.]/g,''));

, span value, .

<div class="balance">
    <span id="balance-value-1">$500.48</span>
</div>

JQuery

var balanceValueElementValue1 = parseFloat($('#balance-value-1').text().replace(/[^0-9\.]/g,''));

JavaScript

var balanceValueElement = document.getElementById("balance-value-1");
var balanceValueElementInnerText = balanceValueElement.innerText;
var balanceValueElementValue1 = parseFloat(balanceValueElementInnerText.replace(/[^0-9\.]/g,''));
+1

All Articles