By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

Adding numerical values in javascript

P: 27
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
Share this Question
Share on Google+
20 Replies


RamananKalirajan
100+
P: 607
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
Expert Mod 5K+
P: 8,639
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

P: 27
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
Expert Mod 5K+
P: 8,639
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

P: 27
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
Expert Mod 5K+
P: 8,639
what is giving you NaN?
Jan 26 '10 #7

P: 27
"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
Expert Mod 5K+
P: 8,639
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

P: 27
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
Expert Mod 5K+
P: 8,639
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

P: 27
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
Expert Mod 5K+
P: 8,639
do you have a page, where I can look at that?
Jan 26 '10 #13

Dormilich
Expert Mod 5K+
P: 8,639
from first glance, p[1] is the wrong URL parameter (letters usually give NaN in parseFloat())
Jan 26 '10 #15

P: 27
OK what would I have to chage it to? Is there an alternative to parsefloat?
Jan 26 '10 #16

Dormilich
Expert Mod 5K+
P: 8,639
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

P: 27
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
Expert Mod 5K+
P: 8,639
of course, parseFloat("ha8") naturally returns NaN.
Jan 26 '10 #19

P: 27
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
Expert Mod 5K+
P: 8,639
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

Post your reply

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