Jump to content

[SOLVED] what is wrong with this?


solarisuser

Recommended Posts

I have <div id=field_50>7000</div> and when I run the JS code below, it doesn't add 80, but appends 80 to 7000.  Also, after it does that, it redirects to another page, but I'm assuming its because of a JS error maybe?

 

function update(i){
var oldone=document.getElementById('field_' + i).innerHTML;
document.getElementById('field_' + i).innerHTML = '';
oldone+= 80;
document.getElementById('field_' + i).innerHTML = oldone;	
}

 

This is called from an <A onclick=update(50);> and works, I used alert() to show that i is being seen (50 in this case)....

 

Thanks in advance!

 

Link to comment
https://forums.phpfreaks.com/topic/53650-solved-what-is-wrong-with-this/
Share on other sites

This happens because the innerHTML returns a string not a number, you can use one of these functions to convert the innerHTML to a number before doing the math

 

oldone = parseFloat(oldone);  // for float numbers

oldone = parseInt(oldone);      // for integers

 

 

 

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.