Noah Skull Weijian
Noah Skull Weijian

Reputation: 129

retrieve html data with javascript

so i have this line of code in my html

    <td class="product-price">
        <span class="amount" id="price">3915.00</span> 
    </td>

how can i that number "3915.00" in my javascript as an integer?

    <script>
    function myFunction()
        {     
            var priceStr = document.getElementById('price').value;
            var priceInt = parseInt(price);              
        }

    </script>

can i do it this way? or are there any alternatives?

Upvotes: 1

Views: 54

Answers (2)

Nenad Vracar
Nenad Vracar

Reputation: 122047

You can use innerHTML or textContent

function myFunction() {
  var priceStr = document.getElementById('price').innerHTML;
  var priceInt = parseInt(priceStr);
  return priceInt;
}

alert(myFunction());
<td class="product-price">
  <span class="amount" id="price">3915.00</span>
</td>

Upvotes: 4

nathan felix
nathan felix

Reputation: 396

Assuming that the price does not come from js in the first place, this is an ok way to get it from the html.

Here is a fixed version of your function though:

function myFunction()
        {     
            return parseInt(document.getElementById('price').innerHTML);    
        }

Upvotes: 2

Related Questions