Jump to content

Adding int to text


gerkintrigg

Recommended Posts

hello. I'm trying to change the total when I click on the button on this page: http://magic2k.com/gd2/shop_cart_add.php?prod=4

 

I want to add 6 to the value of the text, but it just says "NaN", which I understand to mean that I am mixing text and number elements. So how can I make the existing text within the span element an integer?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/144073-adding-int-to-text/
Share on other sites

I would really suggest making a function for this instead of putting the JS inline.

 

Anyway, you have multiple problems:

 

1.  A SPAN does not have a value. You need to use innerHTML to get the current value

2. You need to "cast" the value as a number so it doesn't get interpreted as text. Use parseFloat().

 

Try this

onClick="document.getElementById('total').innerHTML = (parseFloat(document.getElementById('total').innerHTML) + 6);"

Link to comment
https://forums.phpfreaks.com/topic/144073-adding-int-to-text/#findComment-756057
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.