|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Other - Adding numbers automaticly
I would just like to know how to add 2 + numbers and have them go to set input box in an HTML form.
THanx |
|
#2
|
||||
|
||||
|
adding numbers
You can not do this "automatically", the script has to repond to an "event" such as onchange, onclick, etc.
I'm guessing you want to do something like have 3 fields, 1st num, 2nd num and total. First, create the mark-up for your form giving each input an id. <input type="text" id="input1" ... Then have the script get the value var txtval1 = document.getElementById('input1'); var num1 = txtval1.value; .... var total = num1 + num2; var txtval3 = document.getElementById('input3'); txtval3.value = total;
__________________
WP plugins - Error Reporting, Clean Options http://www.mittineague.com/dev/er.php http://www.mittineague.com/dev/co.php |
|
#3
|
|||
|
|||
|
Sorry but my javascript skill is very limited Im not exactly sure how to place this. what I need is to have 3 different totals for dollar amounts. Total Drawer, Starting next day money and bag money. I need all of those to be calculated. Also since you can subtract the total drawer ot next day stat money, I would kinda need a subtraction code too.
Sorry if i'm being a pain |
|
#4
|
||||
|
||||
|
where to put script
I find it convenient to put the javascript in the page's head section while I'm working on it, and then if it's used in several pages to move it into a seperate .js file. Since you're new to javascript, I think it would be best for you to break what you want to do into smaller "pieces" and when you get those working add more to it. That way you won't get overwhelmed trying to figure out the whole thing at once.
Maybe instead of getting the input field values, try "hard-coding" 2 numbers to add and get them to display in a text input. Very basic I know, but it will get you started and help make you feel cormfortable working with the DOM. |
|
#5
|
|||
|
|||
|
Hello hello, I have gotten it to work... Kinda this is my code:
PHP Code:
Everything adds somewhat ok, but if I have decimals in my amount. If i had an amount of 1.33 in pennies, it would just leave it as a dollar. Its ok if it doesn't total automatically but the decimals won't work. |
|
#6
|
|||
|
|||
|
Hi ventu,
try parseDouble instead of parseInt all the best |
|
#7
|
|||
|
|||
|
Quote:
Hey thanx, it was actually parseFloat that got me what I need. Till I had my co-workers do a test run. For some reason This problem ( .12 + .55 + 5.10 + 7.25 + 50 + 50 + 100 = 239.0199999998) But my calculator says it equals 213.02. As you can see from my javascript its layed out right, unless my substacting is 'bleeding' into the addition but then the amount woul be lower. I've attempted to use .toFixed(2), on my totald, totalb, totals bt still isnlt working right. Unless someone would like to help me configure this code the correct way. or at least point me to a good tutorial that would be great. Thanx all |
|
#8
|
|||
|
|||
|
Alright, I have pin pointed the problem. If I have over .99 cents, or i guess 3 int (if you don't a '.' as one) thats when it starts to go weird.
Here is an image: ![]() |
|
#9
|
|||
|
|||
|
bump!!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Other - Adding numbers automaticly |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|