1

I am trying to get 1.99 as number to run calculations. The dollar sign is a dynamic value.

$(function() {
const x = $('#test').text();
console.log(x);
console.log(Number(x) + Number(x));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="test">
  <span>$</span>1.99
</span>
miguelbp87
  • 67
  • 6

1 Answers1

1

Replace the content in the span tag and use what remains.

$(function() {
  const x = $("#test").text().replace($('#test > span').text(), "");  
  console.log(x);
  console.log(Number(x) + Number(x));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="test">
  <span>$</span>1.99
</span>
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • Or use the solutions from the dupe target... Why the answer, when there's a dupe? – Andreas Mar 16 '22 at 13:06
  • 1
    @Andreas probably because this answer the op's issue directly and better than the ten year old answer in the duplicate which suggests making an extended function. – DreamTeK Mar 16 '22 at 13:17
  • Don't you think OP would be able to extract the relevant part? You could also just add your answer on the dupe to consolidate the possible ways in one question. Almost like duplicates are there for a reason... – Andreas Mar 16 '22 at 13:55