1

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?

1
  • span does not have a value property. get it's "textContent" and parse that into an INT, or FLOAT in your case. Commented Apr 10, 2016 at 14:32

2 Answers 2

4

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>

Sign up to request clarification or add additional context in comments.

1 Comment

It's good to note that parseInt will return NaN if the first character cannot be converted to a number
2

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);    
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.