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.
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.
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
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.![]()
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Let's see...
I tried:
It gave me 1+1 (of course!)Code:document.session.timeschanged.value = "1+1"
And tried:
It gave me 11Code:document.session.timeschanged.value = document.session.timeschanged.value+1
And tried:
I tried:
It gave me 1+1 as well...Code:document.session.timeschanged.value = "1"+"1"
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![]()
Last edited by tech_support; 05-31-2006 at 06:48 AM. Reason: A boo-boo! :)
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
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:Try this one:Code: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.Code:document.forms['session'].elements['timechanged'].value++;
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
If it did, then there would be no need to use itsomething++; something--; and that should give you the original, but as a number.![]()
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Thanks for your help guys!
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
Bookmarks