Adding numerical values in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stylishjm
    New Member
    • Jan 2010
    • 27

    Adding numerical values in javascript

    OK heres the code
    Code:
       if ( p[0] == 'price2' ) {
         document.getElementById("3").innerHTML = p[1];
       [U]  document.getElementById("4").innerHTML = p[1] + 5;
         document.getElementById("5").innerHTML = p[1] + 15;[/U]
         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.

    Last edited by Dormilich; Jan 25 '10, 03:10 PM. Reason: fixed [code] tags
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Use parseInt()
    Code:
    document.getElementById("3").innerHTML = parseInt(p[1]);
    document.getElementById("4").innerHTML = parseInt(p[1]) + 5;
    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

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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().

      Comment

      • stylishjm
        New Member
        • Jan 2010
        • 27

        #4
        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.getEle mentById("4").i nnerHTML = 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
        Code:
        document.getElementById("5").innerHTML = parseFloat(p[1]).replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ')+15;
        and the results come up as NaN


        (by the way please dont press book as im using a trial formmail account)
        Last edited by Dormilich; Jan 25 '10, 05:07 PM. Reason: Please use [code] tags when posting code

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Update: Just tried with the ones without the .replace and it only shows the orignal value with nothing added. Then tried

          Code:
          document.getElementById("5").innerHTML = parseFloat[B]([/B]p[1][B])[/B].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:
          Code:
          document.getElementById("5").innerHTML = parseFloat[B]([/B]p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ')[B])[/B]+15;

          Comment

          • stylishjm
            New Member
            • Jan 2010
            • 27

            #6
            still giving me NaN!

            Code:
                 if ( p[0] == 'price2' ) {
                 document.getElementById("3").innerHTML = p[1];
                 document.getElementById("77").innerHTML = p[1];
                 document.getElementById('77').value = document.getElementById('3').innerHTML
                 document.getElementById("4").innerHTML = parseFloat(p[1])+ 5;
                 document.getElementById('4').value = document.getElementById('3').innerHTML
                 document.getElementById("5").innerHTML = parseFloat(p[1])+ 15;
                 document.getElementById('5').value = document.getElementById('3').innerHTML
                 
                 document.getElementById("3").innerHTML = p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ');
                 document.getElementById("77").innerHTML = p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' ');
                 document.getElementById("4").innerHTML = parseFloat(p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+5;
                 document.getElementById("5").innerHTML = parseFloat(p[1].replace('+', ' ').replace('%C2%A3', '£').replace(' UK', ' '))+15;

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              what is giving you NaN?

              Comment

              • stylishjm
                New Member
                • Jan 2010
                • 27

                #8
                "3" and "77" are fine but "4" and "5" (which use the parsefloat( ) ) give the NaN

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  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.

                  Comment

                  • stylishjm
                    New Member
                    • Jan 2010
                    • 27

                    #10
                    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

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      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[]?

                      Comment

                      • stylishjm
                        New Member
                        • Jan 2010
                        • 27

                        #12
                        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

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          do you have a page, where I can look at that?

                          Comment

                          • stylishjm
                            New Member
                            • Jan 2010
                            • 27

                            #14

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              from first glance, p[1] is the wrong URL parameter (letters usually give NaN in parseFloat())

                              Comment

                              Working...