I have an ad I'm working on that displays user inputs from another page but since these inputs can be large numbers, I need to apply JS to format them to be $1K instead of $1000, $1M instead of $1,000,000 and so on. I've tried a few scripts that work in the console but aren't connecting with the HTML that has a value (that I input just to test the JS). I need to make sure this will work on whatever's inside the span tag since on the actual ad there will be a dynamic value that is pulling in whatever the user's input was. I'm pretty sure the issue is in regards to how I'm calling the value inside the first span with the #old id/ .current class, but I've tried several iterations of how to target that value and am getting further from the fix.
I have two JS scripts that are commented out currently, Attempt 1 and Attempt 2 and I'm wondering if it is in relation to the variable that is being defined before the function in Attempt 2 or if there's something missing at the end of either to have the script applied to the value I'm targeting.
Here's the codepen I have so far: http://codepen.io/linzgroves/pen/KgGPGX
If anyone has suggestions on what to adjust here, that'd be super helpful. Thank you!
P.S. I have looked at (and tried) several of the similar questions that have been asked about JS number formatting (such as this) but my issue seems to be more a matter of me incorrectly targeting the value within the span tag and not with the JS itself. However, if there is a similar question related to targeting elements correctly, I'd be happy to pointed towards that as well!
Also, let me know if there's additional information I need to provide.
Thanks!
<div id="container">
<div id="inner">
<div id="message_container">
<div id="roi-headline">
<h2>Cool Header</h2>
<h1>An even cooler headline</h1>
</div>
<div id="roi-values">
<div id="roi-value-1">
<div id="roi-value-1-graph" style="width:75%;">
<em>from</em> <sup>$</sup>
<span id="old" class="current">
100000
</span>
</div>
</div>
<div id="roi-value-2">
<div id="roi-value-2-graph" style="width:100%;">
<em>to</em>
<span><sup>$</sup>15<sup>K</sup></span>
</div>
</div>
</div>
<div id="button">Learn More</div>
</div>
</div>
</div>
Edit: Updated the following var to include .innerText
/* Attempt 1 */
var num = document.getElementById("old").innerText;
function nFormatter(num) {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';
}
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
}
return num;
};
/* Attempt 2 */
var value = "span.current";
function prettifyNumber(value) {
var thousand = 1000;
var million = 1000000;
var billion = 1000000000;
var trillion = 1000000000000;
if (value < thousand) {
return String(value);
}
if (value >= thousand && value <= 1000000) {
return Math.round(value/thousand) + 'k';
}
if (value >= million && value <= billion) {
return Math.round(value/million) + 'M';
}
if (value >= billion && value <= trillion) {
return Math.round(value/billion) + 'B';
}
else {
return Math.round(value/trillion) + 'T';
}
};