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
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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.