Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-29-2011, 07:22 PM   PM User | #1
sherlockturtle
Regular Coder

 
Join Date: May 2011
Posts: 357
Thanks: 23
Thanked 1 Time in 1 Post
sherlockturtle can only hope to improve
+=

how can you make a += work so if you add 5+5 with += it would come out as 55 how would you make it come out as 10?
sherlockturtle is offline   Reply With Quote
Old 07-29-2011, 10:59 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
It will automatically come out as 10 if you make sure that both sides of the "+=" are numeric values. If you (for example) read the value of an input box, it will always be a string (even if you entered a number). You'll have to convert it into a numeric value before adding 5 to it

Example
Code:
var myValue = document.getElementById('myinput').value;
myValue = Number(myValue); // convert it to a numeric value if possible, otherwise it will be "NaN"
myValue += 5;   // mathematically add 5 to this value

Last edited by devnull69; 07-29-2011 at 11:03 PM..
devnull69 is offline   Reply With Quote
Old 07-29-2011, 11:09 PM   PM User | #3
sherlockturtle
Regular Coder

 
Join Date: May 2011
Posts: 357
Thanks: 23
Thanked 1 Time in 1 Post
sherlockturtle can only hope to improve
thanks
sherlockturtle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:13 PM.


Advertisement
Log in to turn off these ads.