Jump to content

Very simple var question


Fire Dragon

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...