'I'm trying to display a number variable on screen after $
I am trying to display a number. The number is in a variable because it can change. I'm trying to make something that displays how much money you have and I also wanna add "$" in front of the number. Here is what I have tried:
HTML:
<p id="money"> $ </p>
JS:
var money = 0;
document.getElementById('money').innerHTML = money;
Solution 1:[1]
You're close, but if you're going to do this using JS then you should set it all inside the JS using a template literal. It will look a little funky because you are first using the $
symbol as an actual string '$'
, then you invoke the template literal using the ${}
syntax like this:
var money = 0;
document.getElementById('money').innerHTML = `$${money}`;
<p id="money"></p>
Solution 2:[2]
You can also use append with jQuery if it only happens once:
<script>
$(document).ready(function () {
var money = 0;
$("#money").append(money);
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="money">$</p
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | maxshuty |