View Full Version : How to add in a form
tech_support
05-31-2006, 03:11 AM
Hi,
How do you add two numbers in a form?
I tried it and it went like 11
When i said it to add the two numbers together.
djr33
05-31-2006, 04:11 AM
the + (plus) symbol in javascript means combine...
"some"+"thing" = "something"
therefore, you seem to be adding two strings together... the string "1" and the string "1".
But if you were using an integer or other numerical variable, it would do the math operation.
One way to know is if you're typing "1"+"1" vs. typing 1+1. The first will give 11, the second 2. The quotes make it be a string.
I'm not too familiar with variable types in javascript, but just change your strings to numbers... either by not using quotes or by setting the variable type of what you're using... user input will probably default to a string, not number.
Good luck. Hope this at least points you in the right direction.
Since I'm not an expert at JS, some of the details might be slightly off, but I'm almost positive the general idea is right. :)
tech_support
05-31-2006, 06:40 AM
Let's see...
I tried:
document.session.timeschanged.value = "1+1"
It gave me 1+1 (of course!)
And tried:
document.session.timeschanged.value = document.session.timeschanged.value+1
It gave me 11
And tried:
I tried:
document.session.timeschanged.value = "1"+"1"
It gave me 1+1 as well...
What am I suppose to do?
I want the user to click a button and it adds one. Like the current value is "1" and i want the user to click a button and it adds "1" so it displays "2" not "11"
I'm not the expert in JS as well, but at least it will do if i'm only 14 :)
For a start, you should never assume that forms will be available as properties of the document element. You should be using the forms and elements collections, like so:
document.forms['session'].elements['timechanged'].valueTry this one:
document.forms['session'].elements['timechanged'].value++;+ is both a string and a number operation, so the parser could easily be forgiven for not performing type conversion there. In fact, it's probably not supposed to. ++, the post-increment operator, however, is purely a numerical operator, so the parser should be forced to convert the string to a number.
djr33
05-31-2006, 04:58 PM
Listen to what Twey said... that should do it.
As for your last post... each example there is wrong.
"1+1":
Things in quotes are just strings... they aren't evaluated... even operators, etc... just like "dosomething()" would just be a string, not a function.
document.session.timeschanged.value+1:
again... your variable is, as user input, by default, a string... meaning you're adding "1" to the end of the string "1".... so... 11.
"1"+"1":
Yeah. Two strings.
Just 1+1 with NO quotes would give you the right answer.
However, that doesn't help with the user input thing.
the something++ method should work.
If nothing else, you could just cheat and do:
something++; something--; and that should give you the original, but as a number. Maybe... not sure exactly how this works.
something++; something--; and that should give you the original, but as a number.If it did, then there would be no need to use it :)
tech_support
05-31-2006, 11:25 PM
Thanks for your help guys!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.