I am trying to change the styling of numbers.
//test for success . . . do not remove
if(document.getElementById('number').innerHTML !== "1000"){
alert('Wrong answer, you changed the number!!!');
}
<p id="number">1000<p>
<p>The 1000 above should show up like 1 000<p>
Try something like this.
const numberWithSep = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "<div class='sep'></div>");
}
var num = numberWithSep('12345678');
document.getElementById("number").innerHTML = num;
//retrive like this
console.log(document.getElementById("number").innerText);
.sep {
display: inline-block;
padding: 10px;
}
<p id="number"><p>
If you want the separator as comma (,) just change the div to ,
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