Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jQuery get n child value outside of html tag

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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading