473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding numerical values in javascript

OK heres the code
Expand|Select|Wrap|Line Numbers
  1.    if ( p[0] == 'price2' ) {
  2.      document.getElementById("3").innerHTML = p[1];
  3.      document.getElementById("4").innerHTML = p[1] + 5;
  4.      document.getElementById("5").innerHTML = p[1] + 15;
  5.      document.getElementById("3").innerHTML = p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ');
Basically the code underlined is what I need help with.
The p[1] is the price taken from a previous page and what I need it to do is to add it to the value. At the moment it is just adding iti to the end of the number. For example if p[1] is 10, then on the first underlined line the result would be 105, the second it would show 1015.

http://www.edgwarelocalcars.co.uk/Booking2.html
Jan 25 '10 #1
20 3011
RamananKalirajan
608 512MB
Use parseInt()
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("3").innerHTML = parseInt(p[1]);
  2. document.getElementById("4").innerHTML = parseInt(p[1]) + 5;
  3. document.getElementById("5").innerHTML = parseInt(p[1]) + 15;
Whatever the value you fetch from input,it will be considered as a string. To convert string to an integer use parseInt()

Thanks and Regards
Ramanan Kalirajan
Jan 25 '10 #2
Dormilich
8,658 Expert Mod 8TB
that’s silent type casting, since the + operator is used for addition and concatenation. try 5 + p[1], or if that doesn’t help use parseFloat() or parseInt().
Jan 25 '10 #3
Ok great, now with the .replace lines in my original post, would I have to enter the parseInt and the + whatever amount or would that just apply for the
document.getElementById("4").innerHTML = p[1] for example.

Update: Just tried with the ones without the .replace and it only shows the orignal value with nothing added. Then tried
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("5").innerHTML = parseFloat(p[1]).replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ')+15;
and the results come up as NaN
http://www.edgwarelocalcars.co.uk/Bo...e2=%C2%A310.00

(by the way please dont press book as im using a trial formmail account)
Jan 25 '10 #4
Dormilich
8,658 Expert Mod 8TB
Update: Just tried with the ones without the .replace and it only shows the orignal value with nothing added. Then tried

Expand|Select|Wrap|Line Numbers
  1. document.getElementById("5").innerHTML = parseFloat(p[1]).replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ')+15;
and the results come up as NaN
naturally. replace() is not a method of the Number object. change your parentheses:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("5").innerHTML = parseFloat(p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+15;
Jan 25 '10 #5
still giving me NaN!

Expand|Select|Wrap|Line Numbers
  1.      if ( p[0] == 'price2' ) {
  2.      document.getElementById("3").innerHTML = p[1];
  3.      document.getElementById("77").innerHTML = p[1];
  4.      document.getElementById('77').value = document.getElementById('3').innerHTML
  5.      document.getElementById("4").innerHTML = parseFloat(p[1])+ 5;
  6.      document.getElementById('4').value = document.getElementById('3').innerHTML
  7.      document.getElementById("5").innerHTML = parseFloat(p[1])+ 15;
  8.      document.getElementById('5').value = document.getElementById('3').innerHTML
  9.  
  10.      document.getElementById("3").innerHTML = p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ');
  11.      document.getElementById("77").innerHTML = p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ');
  12.      document.getElementById("4").innerHTML = parseFloat(p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+5;
  13.      document.getElementById("5").innerHTML = parseFloat(p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+15;
Jan 26 '10 #6
Dormilich
8,658 Expert Mod 8TB
what is giving you NaN?
Jan 26 '10 #7
"3" and "77" are fine but "4" and "5" (which use the parsefloat( ) ) give the NaN

http://www.edgwarelocalcars.co.uk/Bo...e2=%C2%A311.60
Jan 26 '10 #8
Dormilich
8,658 Expert Mod 8TB
line 5/7 or 12/13?

and what is p[1]?

PS. terminate every statement with a semi-colon and technically, IDs must not start with a number.
Jan 26 '10 #9
and what is p[1]?
params[p[0]] = p[1];

line 5/7 or 12/13?
Both im guessing. Im not too good with code so if possible, you will have to look at the source code of the link i posted in the previous post
Jan 26 '10 #10
Dormilich
8,658 Expert Mod 8TB
Both im guessing. Im not too good with code so if possible, you will have to look at the source code of the link i posted in the previous post
look it up in the error console

and what is params[]?
Jan 26 '10 #11
Nothing coming up on error console and params[] Im not sure what it is or what it does but someone who wrote the code for me had it there for a reason. If you read the whole thread everything was working fine untill I needed to use the parseint / parsefloat to get the values to add
Jan 26 '10 #12
Dormilich
8,658 Expert Mod 8TB
do you have a page, where I can look at that?
Jan 26 '10 #13
Dormilich
8,658 Expert Mod 8TB
from first glance, p[1] is the wrong URL parameter (letters usually give NaN in parseFloat())
Jan 26 '10 #15
OK what would I have to chage it to? Is there an alternative to parsefloat?
Jan 26 '10 #16
Dormilich
8,658 Expert Mod 8TB
the alternative is not using p[1], but the correct value.

the incorrect thing is: p[1] = "ha8+7ej%C2%A0UK"
Jan 26 '10 #17
so I would put this instead for example?
document.getElementById("5").innerHTML = parseInt(price2.replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+15;
Edit: Still comes up with NaN
Jan 26 '10 #18
Dormilich
8,658 Expert Mod 8TB
of course, parseFloat("ha8") naturally returns NaN.
Jan 26 '10 #19
in the example I showed in the post before this one, the value is 'price2' which is the id of the variable passed over that I need to be "parsed" yet it still doesnt work. It doesnt contain any addresses or what not, just numbers and "£" sign.
Jan 26 '10 #20
Dormilich
8,658 Expert Mod 8TB
tip: check your html

works neither in Chrome nor Safari
Error: NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7 - Booking2.html:189

findings from Dragonfly:
p[1] = "%A311.60" which is parsed into NaN, or in other words, your replace search string doesn’t match.
Jan 26 '10 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: yukatan | last post by:
Maybe it's a newbie question, but if I have two strings, let's say s1="4" and s2="5", how can I get a new string of value "9", that is, add the two numbers. If I type: var newStr = s1 + s2 all I...
1
by: Frank | last post by:
I have a large form, that has text boxes of numbers in rows and columns. I need to sum the values in the columns, and put the total at the bottom of the column. But I also need to sum the values in...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: Kyle Friesen via AccessMonster.com | last post by:
Hello, I have two fields (Customer and Product) which I need to track or assign an "ID number" to. For example, when Customer A has its first Product Y entry, then AY1. When Customer B has its first...
2
by: Martin McCormick | last post by:
Most of the C programming I write manipulates strings so I haven't had much experience with numerical functions and passing variables in and out which brings me to a laughably simple problem that I...
5
by: rdemyan via AccessMonster.com | last post by:
I have a table with about 80 fields. I'm using an import process to populate the table. It works fine, except for the following: Users generally don't specify values for a lot of numerical...
9
by: joanne matthews (RRes-Roth) | last post by:
I'm getting different results when I add up a list of floats depending on the order that I list the floats. For example, the following returns False: def check(): totalProp=0 inputs= for each...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.