1

I have the numeric value in a span (.amount), in hierarchy with other divs (.coins and .description).

What I would like to do:

1 - Copy the contents of <.amount> to a variable called $coins

2 - Update a var $total with the sum of $coins + $wallet

3 - Copy the var $total and show it in a Span with ID #total

Below is the idea of the process, but it is not a code itself.

Very thanks!

<div class="coins">
    <div class="description">
        <span class="amount">152</span>
    </div>
</div>

<script>
    var coins = 152 //content of the hierarchy class ".coins .amount"
    var wallet = 1000
    var total = coins + wallet
</script>

Your Total: <span id="total">$total</span>
Jonatas
  • 13
  • 2

1 Answers1

0
  1. How to get content of span
  2. How to set content in span

    <div class="coins">
        <div class="description">
            <span id="amount">152</span>
        </div>
    
        <p>Your Total: <span id="total"></span></p>
    </div>
    
    <script>
            var coins = Number(document.getElementById("amount").innerText)
            var wallet = 1000
            var total = coins + wallet
    
            document.getElementById("total").innerText = total
    </script>
    
Adrien Leloir
  • 513
  • 4
  • 8
  • Thanks Adrien!! Is there a possibility to use the class, without changing to ID? And inform the hierarchy, something like "document.getElement... (".coin. amount")" Would it work that way? – Jonatas Apr 29 '20 at 17:21
  • Yes you can use query selector `document.querySelector(".coin .amout").innerText` – Adrien Leloir Apr 29 '20 at 17:29