Fire Dragon Posted February 21, 2006 Share Posted February 21, 2006 Okay,I tried find example for this kind code,but after I searched half or one hour,I decided only ask it here.So,this may be stupid question,but how I can plus variable to another variable?Like this: <script type="text/javascript">var number1=100;var number2=100;function increase(){ number1 + number2 document.getElementById('number').value=number2;}</script></head><body><input type="button" value="Increase" onclick="increase()" /><p>Variable: <input type="input" value="0" id="number" /></p> I thought that you can only create number1 + number2 kind code,but looks like it won't work.I also want it show in input field.Now,when I tested it,field shows only "100",when I want it shows "200".That because 100 + 100=200Thanks for help!I really appreciate it,because I need this kind code for my script. Link to comment Share on other sites More sharing options...
Pollux Posted February 21, 2006 Share Posted February 21, 2006 Your code does work, you are adding the two variables together. The problem is, you're not assigning the result to anything, so it effectively just disappears. If you want to update the number2 variable then you need to do that explicitly like this: number2 = number1 + number2; Link to comment Share on other sites More sharing options...
scott100 Posted February 21, 2006 Share Posted February 21, 2006 try: <script type="text/javascript">var number1=100;var number2=100;function increase(){var num= -number1 -number2;var num= -numdocument.getElementById('number').value=num;}</script></head><body><input type="button" value="Increase" onclick="increase()" /><p>Variable: <input type="input" value="0" id="number" /></p> Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 21, 2006 Author Share Posted February 21, 2006 Yep.I understood what Pollux posted,but Scott100's code was little confusing Thanks anyway. Link to comment Share on other sites More sharing options...
scott100 Posted February 21, 2006 Share Posted February 21, 2006 Yep.I understood what Pollux posted,but Scott100's code was little confusing Thanks anyway. <{POST_SNAPBACK}> I did work though??? Link to comment Share on other sites More sharing options...
Chocolate570 Posted February 21, 2006 Share Posted February 21, 2006 Too complex, man. Just use the eval() function. numberfinal=eval("number1+number2"); Will give you 200.Also, you could use number1+=number2 which will add number1 and number 2 and put the answer in number1. Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 21, 2006 Author Share Posted February 21, 2006 I did work though??? <{POST_SNAPBACK}> Yes,you did!And it worked too Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now